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
I tried the below XPath..on html page its showing the element but when putting on selenium code it's showing the error 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
Please help me to find xpath for first searched product.
Thanks in Advance.
CodePudding user response:
You can directly use
//li[starts-with(@class,'octopus-pc-item')]
this does have 26 entries and is not unique in HTML, however, findElement
will return the first matching node.
Furthermore, you can do indexing like below
(//li[starts-with(@class,'octopus-pc-item')])[1]
and [2]
for the second product and so on... for other products
Perform click like below
Using
ExplicitWaits
wait = new WebDriverWait(driver, Duration.ofSeconds(30)); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[starts-with(@class,'octopus-pc-item')]"))).click();
Using
findElement
Thread.sleep(3000); driver.findElement(By.xpath("(//li[starts-with(@class,'octopus-pc-item')])[1]")).click();