简体   繁体   中英

Python Selenium Click button

I am trying to use Selenium to click the add to basket button as shown in the code below:

import time
from selenium import webdriver

#Lauch Chrome driver
b = webdriver.Chrome('chromedriver')  # Optional argument, if not specified will search path.
b.get("https://www.currys.co.uk/gbuk/computing/laptops/laptops/asus-vivobook-k403ja-14-laptop-intel-core-i5-512-gb-ssd-silver-blue-10208166-pdt.html")

b.find_element_by_class_name("Button__StyledButton-iESSlv").click()

However, I got the error below saying the element is not clickable:

ElementClickInterceptedException: Message: element click intercepted: Element <button type="submit" class="Button__StyledButton-iESSlv iQXRSE Button-dtUzzq kHUYTy">...</button> is not clickable at point (857, 60). Other element would receive the click: <div class="onetrust-pc-dark-filter ot-fade-in" style="z-index:2147483645"></div>
  (Session info: chrome=89.0.4389.90)

Anyone has any idea why that's the case?

wait = WebDriverWait(b, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#onetrust-accept-btn-handler"))).click()

elem=wait.until(EC.presence_of_element_located((By.XPATH,"//button[contains(.,'Add to basket')]")))
driver.execute_script("arguments[0].click();", elem)

Remove the popup and click the add to basket button.

Import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

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