【问题标题】:Element not visible in Chrome and IE - Selenium Java元素在 Chrome 和 IE 中不可见 - Selenium Java
【发布时间】:2018-07-24 08:51:59
【问题描述】:

我正在尝试在 Eclipse IDE 中使用 Selenium 和 Java 执行自动化测试。

我正在使用 chrome 浏览器中的“检查元素”选项查找 xpath。然而,相同的 xpath 在 Firefox 浏览器中运行良好,但在 chrome 和 IE 中却不行。有人可以帮我在 chrome 和 IE 中解决这个问题吗?它在 Chrome 和 IE 中引发“元素不可见”错误。

【问题讨论】:

  • 问题可能与 XPath 本身有关。可以发一下吗?
  • 例如:我就是这样用的。
  • driver.findElement(By.xpath("//*[@id=\"lenderName\"]")).click();
  • 尝试 driver.findElement(By.xpath("//*[@id='lenderName']")).click();

标签: java selenium


【解决方案1】:

试试这个 xpath 并在它之前添加一个等待,例如

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='lenderName']")));
driver.findElement(By.xpath("//*[@id='lenderName']")).click();

【讨论】:

    【解决方案2】:

    您可以尝试以下选项来查找元素:

    driver.findElement(By.id("lenderName")).click();
    
    driver.findElement(By.cssSelector(".lenderName")).click();
    
    driver.findElement(By.xpath("//*[@id='lenderName']")).click();
    

    如果仍然无法正常工作,则使用 JavascriptExecutor 显式等待:

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

    【讨论】:

    • 当然,但如果有效,请不要忘记接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多