简体   繁体   English

如何通过 Selenium 的 XPath 访问 WebElement?

[英]How to access WebElement by XPath by Selenium?

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.我需要访问该网站上搜索结果的链接( https://www.pibr.org.pl/pl/search/auditor?biegli=1&firmy=1&name=&post_code=&city=Warszawa )并将它们放在WebElement ,但是我无法按课程或任何东西找到它们。 While using xpath :使用xpath

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

I get this error:我收到此错误:

"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?如何访问所有结果链接?

The xpath should be "//div[@class='inner-results firma']" , with quotation marks around the class attribute. xpath应该是"//div[@class='inner-results firma']" ,在class属性周围加上引号。 You should also use findElements to get more than one result您还应该使用findElements来获得多个结果

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

As a side note, variables in Java should start with lower case, MyWebDriver -> myWebDriver作为旁注,Java 中的变量应以小写开头, MyWebDriver -> myWebDriver

You need to put the class name in single quotes, please use the below command to get the links: MyWebDriver.findElement(By.xpath("//div[@class='inner-results firma']")).click();您需要将类名放在单引号中,请使用以下命令获取链接: MyWebDriver.findElement(By.xpath("//div[@class='inner-results firma']")).click();

Though this would click only on the first element of the class, if you want to get all the links and then click on the first link then you can use: MyWebDriver.findElements(By.xpath("//div[@class='inner-results firma']")).get(0).click();虽然这只会单击类的第一个元素,但如果您想获取所有链接,然后单击第一个链接,则可以使用: MyWebDriver.findElements(By.xpath("//div[@class='inner-results firma']")).get(0).click(); and by using this xpath you can click on any link mentioned on the page by sending the index in the get(index) method.通过使用此 xpath,您可以通过在 get(index) 方法中发送索引来单击页面上提到的任何链接。

Kindly below code snippet which will give you links store in weblist:请在下面的代码片段中提供链接存储在网络列表中:

 import java.awt.AWTException; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class Testing { public static WebDriver driver; @Test public void test() throws InterruptedException, AWTException { System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver"); driver = new ChromeDriver(); driver.get("https://www.pibr.org.pl/pl/search/auditor?biegli=1&firmy=1&name=&post_code=&city=Warszawa"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS); List<WebElement> fromDropDwon = driver.findElements(By.xpath("/html/body/div[2]/div/div[2]/div/h3/a")); for (WebElement element : fromDropDwon) { System.out.println(element.getAttribute("href")); } } }

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM