Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im trying to use selenium to select the first result in a search. enter image description here

To click on the first image im using xpath way to find the first result from the search. The code is

driver.findElement(By.xpath("//*[@id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a")).click();

and from using the f12 and then ctrl f tools on the website it shows that I have the correct xpath

enter image description here

However, it is not clicking on the Image. Here is the website that I am trying to test if it's any help. https://www.dunnesstores.com/search?keywords=lamp

question from:https://stackoverflow.com/questions/65945613/selenium-xpath-not-clicking-on-image-link

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
370 views
Welcome To Ask or Share your Answers For Others

1 Answer

To click() on the first result from the search you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    driver.get("https://www.dunnesstores.com/search?keywords=lamp")
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ol.product-list.product-list-main > li a"))).click();
    
  • xpath:

    driver.get("https://www.dunnesstores.com/search?keywords=lamp")
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='onetrust-accept-btn-handler']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ol[@class='product-list product-list-main']/li//a"))).click();
    
  • Browser Snapshot:

dunnesstores


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...