How to find XPath for the first searched element on given page https://www.amazon.com/b/?encoding=UTF8&node=2346727011&bbn=7141123011&ref_=Oct_d_odnav_1040660&pd_rd_w=WJf7p&pf_rd_p=72459b27-e231-4837-b61c-b057ff0c50ac&pf_rd_r=YMKJ7M0AMXW5GER3MMRQ&pd_rd_r=eba15a0a -f59c-4dfd-a693-7c2f7f3416cf&pd_rd_wg=LRrkE
我嘗試了下面的 XPath..on html 頁面,它顯示了該元素,但是在使用 selenium 代碼時,它顯示了錯誤invalid selector: Unable to locate an element with the xpath expression
//div[@id='CardInstance1wPgwM8pHmoJmdnyjYOeWg']//child::div[2]//div[1]//div[1]//div[1]//div[2]//div[@class='a-section a-spacing-none a-spacing-top-small s-title-instructions-style']//span
請幫我找到第一個搜索產品的 xpath。
提前致謝。
uj5u.com熱心網友回復:
你可以直接使用
//li[starts-with(@class,'octopus-pc-item')]
這確實有 26 個條目,并且在 HTML 中不是唯一的,但是,findElement它將回傳第一個匹配的節點。
此外,您可以像下面那樣進行索引
(//li[starts-with(@class,'octopus-pc-item')])[1]
對于第二個產品,依此[2]類推......對于其他產品
執行如下點擊
使用
ExplicitWaitswait = new WebDriverWait(driver, Duration.ofSeconds(30)); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[starts-with(@class,'octopus-pc-item')]"))).click();使用
findElementThread.sleep(3000); driver.findElement(By.xpath("(//li[starts-with(@class,'octopus-pc-item')])[1]")).click();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/468303.html
上一篇:無法使用seleniumwebdriver獲取所有表格元素
下一篇:元素存在時硒找不到元素
