How to locate a span with a specific text in Selenium? (Using Java)

后端 未结 2 1240
你的背包
你的背包 2021-02-04 15:04

I\'m having trouble locating a span element in Selenium using java.

the HTML looks like:

相关标签:
2条回答
  • 2021-02-04 15:19

    Your all xpath are looks OK, Just some syntactically incorrect. you are missing // in your xpath

    The correct xpath are as below :-

    By by = By.xpath("//span[.='Settings']")
    

    Or

    By by = By.xpath("//span[text()='Settings']")
    

    Or

    By by = By.xpath("//div[@class='settings-padding']/span"))
    

    Or you can use cssSelector as :-

    By by = By.cssSelector("div.settings-padding > span"))
    

    Using anyone of the above By locator you can locate element as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(presenceOfElementLocated(by));
    

    Hope it helps...:)

    0 讨论(0)
  • 2021-02-04 15:43

    For the element below

    <span class="test-button__text">
        Test Text
    </span>
    

    The following solution works for me

    driver.find_element_by_xpath("//span[contains(@class, 'test-button__text') and text()='Test Text']")
    
    0 讨论(0)
提交回复
热议问题