简体   繁体   中英

Selenium xpath not clicking on Image link

Im trying to use selenium to select the first result in a search. 在此处输入图像描述

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

在此处输入图像描述

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

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

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#.netrust-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='.netrust-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:

邓尼斯商店

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM