如何通过 Selenium 的 XPath 访问 WebElement? [英] How to access WebElement by XPath by Selenium?

查看:39
本文介绍了如何通过 Selenium 的 XPath 访问 WebElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问指向本网站搜索结果的链接 (https://www.pibr.org.pl/pl/search/auditor?biegli=1&firmy=1&name=&post_code=&city=Warszawa) 并将它们放在 WebElement 中,但我无法按类或任何东西找到它们.使用 xpath 时:

I need to access links to result of search on this website (https://www.pibr.org.pl/pl/search/auditor?biegli=1&firmy=1&name=&post_code=&city=Warszawa) and put them in WebElement, but I cant locate them by class or anything. While using xpath:

MyWebDriver.findElement(By.xpath("//div[@class=inner-results firma]")).click();

我收到此错误:

"Given xpath expression "//div[@class=inner-results firma]" is invalid: SyntaxError: The expression is not a legal expression."

如何访问所有结果链接?

How can I access all result links?

推荐答案

xpath 应该是 "//div[@class='inner-results Firma']",用引号将 class 属性括起来.您还应该使用 findElements 来获得多个结果

The xpath should be "//div[@class='inner-results firma']", with quotation marks around the class attribute. You should also use findElements to get more than one result

MyWebDriver.findElements(By.xpath("//div[@class='inner-results firm']")).click();

附带说明,Java 中的变量应以小写开头,MyWebDriver -> myWebDriver

As a side note, variables in Java should start with lower case, MyWebDriver -> myWebDriver

这篇关于如何通过 Selenium 的 XPath 访问 WebElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆