0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

0
driver = webdriver.Chrome()
driver.get('https://www.dunnesstores.com/search?keywords=lamp')

accept = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
                                                                        "#onetrust-accept-btn-handler")))

accept.click()

driver.find_element_by_css_selector(
    '[class="c-product-card__link"]').click()

You have to first click accept cookies , then find element using class or xpath or css

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Try:

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

With this code you avoid the elements that obscures it and the elements that are not disponible in your actual screen (out of scroll).

MR Mark II
  • 433
  • 3
  • 13