【问题标题】:Unable to click on hyperlink when running selenium webdriver script运行 selenium webdriver 脚本时无法单击超链接
【发布时间】:2018-07-20 11:02:26
【问题描述】:

我一直在尝试点击导致屏幕上弹出的超链接。由于某种原因它不起作用,目前超链接的 html 是这样的:

<a href="javascript:void(0)" class="operating-hours left">Select Hours</a>

尝试使用这个 selenium 代码:​​

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.linkText("Select Hours"))).click(); 

也试过了:

driver.findElement(By.id("operating_hours")).click();

有趣的是,我也没有收到任何错误,只是没有点击超链接。 screenshot after running the script 运行脚本后就是截图了。

而脚本应该已经打开了这个弹出窗口 popup that should open after running the script @DebanjanB 希望能澄清问题。

【问题讨论】:

  • 请提供HTML代码,并将代码格式化为更具可读性,请使用代码块,tnx

标签: selenium selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

根据您共享的 HTML 而不是linkText,您可以使用以下任一方法和Locator Strategies

  • cssSelector:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.operating-hours.left"))).click();
    
  • xpath:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='operating-hours left'][contains(.,'Select Hours')]"))).click();
    
  • 如果您仍然无法在所需元素上调用click(),您可以使用Actions 类,如下所示:

    WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.operating-hours.left")));
    //WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='operating-hours left'][contains(.,'Select Hours')]")));
    new Actions(driver).moveToElement(element).click().build().perform();
    
  • 您也可以使用JavascriptExecutor 接口中的executeScript() 方法,如下所示:

    WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.operating-hours.left")));
    //WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='operating-hours left'][contains(.,'Select Hours')]")));
    (JavascriptExecutor)driver.executeScript("arguments[0].click();", element);
    

【讨论】:

  • 两者都试过了,但都没有改善.. 无论如何感谢@debanjanb
  • @JaffarLone no improvement 没有说明发生了什么错误。你能用你当前看到的代码试验和错误来更新这个问题吗?
  • 好吧,我附上了两张截图,因为我没有从 testNG 或 Eclipse 获得任何错误日志,这就是我所拥有的。
  • @JaffarLone 查看我的答案更新并告诉我状态
  • 第二个选项有效,弹出窗口终于在主屏幕上打开。谢谢:)
【解决方案2】:

以下是通过不同选择器点击链接的几个示例:

点击全文链接

WebElement element = driver.findElement(By.linkText("My Link"));
linkByText.click();

按部分文字点击链接

WebElement element = driver.findElement(By.partialLinkText("First"))
linkByPartialText.click();

使用 XPath 按文本点击链接

WebElement element = driver.findElement(By.xpath("//a[text()='First']"));
linkByTextUsingXPath.click();

使用 XPath 按部分文本点击链接

WebElement element = driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));
linkByPartialTextUsingXPath.click();

如果需要,也可以使用显式等待:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("some_link")));
element.click();

希望这会有所帮助,

【讨论】:

    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    相关资源
    最近更新 更多