为什么Selenium webdriver无法选择某些元素?

时间:2017-07-12 12:41:01

标签: selenium-webdriver selenium-chromedriver

我一直在使用Selenium webdriver很长一段时间。最近我遇到了这个页面:https://www.myagedcare.gov.au/service-finder

enter image description here

看起来非常简单,我尝试使用id locator:bylocation

然而,无论我多久等待元素准备好等待一段时间,或等待元素准备就绪:

    WebDriver driver = getDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(
            ExpectedConditions.elementToBeClickable(By.id("byname")))
            .click();

元素永远不会准备好,我总是得到错误,比如“没有这样的元素:无法找到元素:”

有谁知道发生了什么事?我已经使用定位器很长一段时间了,我不知道这是怎么发生的。

我正在使用chromedriver 2.30和chrome browser v.60。

www.google.com中的这样一个简单的ID定位器并没有给我带来任何麻烦。我测试了它,所以我不认为它是驱动程序或浏览器问题。

由于

日志

net.serenitybdd.core.exceptions.SerenityWebDriverException: Timed out after 10 seconds waiting for visibility of element located by By.xpath: //input[@id='bylocation']
Build info: version: '2.46.0', revision: '61506a4624b13675f24581e453592342b7485d71', time: '2015-06-04 10:22:50'
System info: host: 'yun-PC', ip: '192.168.1.14', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'

Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id='bylocation']"}
  (Session info: chrome=60.0.3112.50)
  (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 20.04 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

2 个答案:

答案 0 :(得分:1)

试试这个xpath,让我知道它是否有效,

//div[@id='edit-search-by--2']//label[normalize-space(text())='Location']/preceding-sibling::input[@type='radio']

也尝试等待元素的可见性,而不是可点击的元素。

修改

以上Xpath是完美的。问题是元素位于框架中。 下面的代码将为您提供点击部分:

     driver.get("https://www.myagedcare.gov.au/service-finder?tab=help-at-home");

    new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[@id='content']")));
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='content']")));


    new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[normalize-space(text())='Location']/preceding-sibling::input[@type='radio']")));

    WebElement location = driver.findElement(By.xpath("//label[normalize-space(text())='Location']/preceding-sibling::input[@type='radio']"));
    WebElement name = driver.findElement(By.xpath("//label[normalize-space(text())='Name']/preceding-sibling::input[@type='radio']"));

    location.click();

    Thread.sleep(3000);
    name.click();

    driver.switchTo().parentFrame();

点击工作视频 - https://www.screencast.com/t/0Fsw7gGZ

答案 1 :(得分:0)

由于Radio Buttons位于input标记内,而不是id使用xpath定位符,如下所示:

    WebDriverWait wait6 = new WebDriverWait(driver, 10);
    WebElement radio_name = wait6.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='byname']/input[@id='byname']")));
    radio_name.click();

如果这回答你的问题,请告诉我。