gpt4 book ai didi

java - 使用 Selenium 在表上迭代

转载 作者:行者123 更新时间:2023-12-02 11:45:35 24 4
gpt4 key购买 nike

我不明白为什么 x-path 在表中不起作用。我正在尝试从我找到 xpath 的网站迭代表,但 Eclipse 告诉我:

"Unable to find element with xpath".

我应该以另一种方式在表中查找 xpath 吗?

网站网址:https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie

我尝试阅读一个报价,例如:

String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//html/body/div[2]/div[4]/div[2]/div[2]/div/div[2]/div[5]/table/tbody/tr[5]/td";
String sCellValue = driver.findElement(By.xpath(xpathResult)).getText();
System.out.print(sCellValue);

我做错了什么?

最佳答案

找到以下代码来迭代表中的所有单元格值。它可能对你有帮助。

String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//table/tbody/tr/td";
List<WebElement> rows = driver.findElements(By.xpath(xpathResult));
List<WebElement> cols=new ArrayList<WebElement>();
for(int i=0;i<rows.size();i++){
System.out.print("Row value: "+i);
cols=rows.get(i).findElements(By.tagName("td"));
for(WebElement col:cols)
System.out.print("cell value "+col.getText());
}

关于java - 使用 Selenium 在表上迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com