【问题标题】:Unable to find a web-element with Selenium无法使用 Selenium 找到 Web 元素
【发布时间】:2020-08-28 15:29:10
【问题描述】:

在 Selenium 中,需要点击网页上的链接 [Google Cloud Platform Pricing Calculator] [第一个链接] 1

我创建了 xpath:

//*[@class = 'gs-bidi-start-align gs-visibleUrl gs-visibleUrl-long']

Google Developer Tool 可以看到它,但 WebDriver 没有。请问我的 Xpath 有什么问题?

代码:

@FindBy(xpath = "//*[@class = 'gs-bidi-start-align gs-visibleUrl gs-visibleUrl-long']") 
WebElement clickingLinkToCalculator; 


public void preTestArrangement(String text) { 
//some other methods
clickingLinkToCalculator.click(); 
}

【问题讨论】:

  • 能否请您说明如何在代码中使用此 xpath?
  • 你好确定@FindBy(xpath = "//*[@class= 'gs-bidi-start-align gs-visibleUrl gs-visibleUrl-long']") WebElement clickLinkToCalculator; public void preTestArrangement(String text) { //其他一些方法 clickLinkToCalculator.click(); }
  • 您的 xpath 会在 Google DevTools 中找到所有链接,而不仅仅是第一个。并且无需将所有类都放入您的选择器中。试试这个:(//*[@class='gsc-url-top'])[1]
  • 对不起,这个:(//*[contains(@class, 'gs-visibleUrl-long')])[1]
  • 谢谢你,Yevhen,它在 Google Developer 工具中运行良好,但我的 Selenium 仍然无法正常工作,我会尽力解决它。无论如何,感谢您的帮助和承诺:)

标签: java selenium-webdriver automation qa


【解决方案1】:

使用 Selenium 进行交互时,此页面布局有点棘手。当然,xpath 可以找到元素,但不能保证找到的元素可以通过自动化进行交互,或者点击它会触发链接。

在这种网络元素深度嵌套的特殊情况下,您必须凭经验找到正确的元素。这个是可点击的:

(//div[contains(@class, 'gs-title')])[1]/a

【讨论】:

    猜你喜欢
    • 2018-03-31
    • 2023-04-01
    • 2021-11-13
    • 1970-01-01
    • 2019-10-26
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多