Selenium xpath 没有点击图像 link

Selenium xpath not clicking on Image link

我正在尝试使用 selenium select 搜索中的第一个结果。

点击第一张图片我使用 xpath 方式从搜索中找到第一个结果。 密码是

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

通过在网站上使用 f12 和 ctrl f 工具,它表明我有正确的 xpath

但是,它没有点击图像。 这是我正在尝试测试的网站是否有帮助。 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()

您必须先单击接受 cookie,然后使用 class 或 xpath 或 css

查找元素

click() 您需要在搜索的第一个结果上归纳 for the elementToBeClickable() and you can use either of the following :

  • 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();
    
  • 浏览器快照:

尝试:

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

使用此代码,您可以避免遮挡它的元素以及实际屏幕中不可靠的元素(超出滚动范围)。