在此页面https://www.bestbuy.ca/en-ca/category/laptops-macbooks/20352 (这是笔记本电脑结果页面,其中列出了许多笔记本电脑,我正在尝试获取这台特定的笔记本电脑)
WebElement -- 'HP 15.6" Laptop - Silver (Intel Core i3-1005G1/256GB SSD/8GB RAM/Windows 10)' using xpath and below is my xpath:
//div[contains(text(),'HP 15.6" Laptop - Silver (Intel Core i3-1005G1/256GB SSD/8GB RAM/Windows 10)')]
这个 xpath 正确地识别了元素,但是当我在 eclipse 中粘贴这个 xpath 时,它会在
中的 15.6(即 15.6\")之后添加反斜杠\xpath("div[contains(text(),'HP 15.6\"笔记本电脑 - 银色 (Intel Core i3-1005G1/256GB SSD/8GB RAM/Windows 10)')]"))
这就是为什么我的代码抛出“未找到元素”异常的原因。谁能帮我解决这个问题。
最佳答案
15.6\"
是正确的 eclipse 中显示的内容。因此它会为您转义嵌入的引号。否则它将在 Eclipse IDE 上给出语法错误。由于页面需要时间而失败的原因加载元素因此失败。
引发 WebDriverWait
() 并等待 elementToBeClickable
() 然后单击。
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'HP 15.6\" Laptop - Silver (Intel Core i3-1005G1/256GB SSD/8GB RAM/Windows 10)')]"))).click();
关于java - 使用 Selenium 和 Java 编写 xpath 的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61343067/