【问题标题】:Unable to find element找不到元素
【发布时间】:2017-01-06 20:13:23
【问题描述】:

我已经写了下面的代码来选择单选按钮,它工作正常,但今天它不工作。请找到代码和相应的错误消息

代码1:

WebDriverWait wait = new WebDriverWait(driver,10);
    WebElement radio = wait.until    (ExpectedConditions.presenceOfElementLocated(By.id("0_2485A_StartDate")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click()", radio);

错误1: '线程“main” org.openqa.selenium.TimeoutException 中的异常:等待 10 秒后超时,等待存在以下元素:By.id: 0_2485A_StartDate'

代码2:

 WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement radio = wait.until    (ExpectedConditions.presenceOfElementLocated(By.id("Radio_0_2485A")));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click()", radio);

错误2: '线程“main”中的异常 org.openqa.selenium.TimeoutException:等待 10 秒后超时,等待由以下位置的元素存在:By.id:Radio_0_2485A'

代码3:

WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement radio = wait.until(ExpectedConditions.elementToBeClickable(By.id("0_2485A_StartDate")));
        radio.click();

错误3: '线程“main”中的异常 org.openqa.selenium.TimeoutException: 等待元素可点击 10 秒后超时:By.id: 0_2485A_StartDate'

代码4:

WebDriverWait 等待 = 新的 WebDriverWait(驱动程序,10); WebElement radio = wait.until(ExpectedConditions.elementToBeClickable(By.id("Radio_0_2485A"))); radio.click();

错误4: '线程“main” org.openqa.selenium.TimeoutException 中的异常:等待元素可点击 10 秒后超时:By.id: Radio_0_2485A'

HTML:

    <th class="radio">
    <input id="0_2485A_StartDate" type="hidden" value="18/12/2015 00:01:00">

    <input name="Products[0].ProductCode" title="5 Year Fixed Rate Until 28/02/2021 with £999 Fee" id="Radio_0_2485A" type="radio" value="2485A">
    <label for="Radio_0_2485A">
       5 Year Fixed Rate Until 28/02/2021 with £999 Fee
  </label>
    </th>

请提出建议。

【问题讨论】:

  • 不,它不是动态生成的。

标签: selenium-webdriver


【解决方案1】:

我无法复制您的实际 Web 浏览器场景,而只是尝试复制您的 html 代码并放入一个简单的 html 模板。我看到您几乎尝试了所有方法。如果可能的话,您能否提供其他人可以查看该页面的网址。

我试过这样:

driver.findElement(By.xpath("//input[@value='2485A']")).click();

它奏效了。让我知道这是否有帮助。

【讨论】:

  • Sandipan...我试过 driver.findElement(By.xpath("//input[@value='2485A']")).click();它不工作......
  • 能不能给个网址,方便大家检查机器
【解决方案2】:

presenceOfElementLocated 只是检查存在于DOM 中的元素,因此如果您将其与ExpectedConditions 一起使用,您的代码将可以正常工作。

但正如我所见,异常堆栈跟踪表明您正在使用elementToBeClickable,实际上elementToBeClickable 用于等待元素可见和可点击,而定位的元素被隐藏并且永远不可见。

您正在定位隐藏元素,这就是您遇到麻烦的原因。

您应该尝试如下定位实际的单选元素:-

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.elementToBeClickable(By.id("Radio_0_2485A")))
radio.click()

【讨论】:

  • 当我尝试使用下面的代码时 WebDriverWait wait = new WebDriverWait(driver,10); WebElement radio = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("0_2485A_StartDate"))); ((JavascriptExecutor)driver).executeScript("arguments[0].click()", radio);
  • 我已经告诉过你,你正在使用 elementToBeClickable 定位隐藏元素,你应该尝试使用这个 I'd 0_2485A_StartDate,它是可见的,并且是无线电元素的实际 Id
  • Saurabh...我认为您指的是code3。请查看code3。我在使用code3时也遇到错误..
  • Saurabh...如何检查元素是否在框架内?我的朋友也在使用相同的代码(code1)并且它在他的机器上工作...
  • 如果具有相同元素的相同代码在不同的机器上工作,那么它可能不在内部和框架/iframe,任何检查框架/框架是否存在只需在该页面按 f12 进入浏览器控制台并执行此操作document.getElementsByTagName("frame")document.getElementsByTagName("frame") 并检查是否在框架中找到结果。
猜你喜欢
  • 2014-01-27
  • 2018-03-08
  • 2017-08-30
  • 2021-04-24
  • 2017-12-21
  • 2019-01-02
  • 2021-08-17
  • 2021-10-24
  • 2017-05-06
相关资源
最近更新 更多