简体   繁体   中英

Trying to click an element on a webpage using selenium

I'm trying to write a script to fill out a form but I'm struggling to click the free sim link, I have tried using multiple different identifiers but can't seem to get any to work. Greatly appreciate any help! Thank you

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://mobile.lebara.com/gb/en/free-sim")

driver.implicitly_wait(5)

cookie = driver.find_element_by_id("onetrust-accept-btn-handler")
freesim = driver.find_element_by_class_name("product-item payAsYouGoProductListerItem clickable")

actions = ActionChains(driver)
actions.click(cookie).perform()
driver.implicitly_wait(5)
actions.click(freesim).perform()
  1. You have to deal with "accept cookies" banner (click on it, you don't need actions, only EC then click)
  2. replace all implicit waits in your code with expected_conditions

(In case all these recommendations don't work for you have to update your question with code and selenium exceptions)

You need to click "Accept cookies" or execute JS code to hide "Your cookies" overlay and then search for link.

driver.implicitly_wait(5)

cookie = driver.find_element_by_id("onetrust-accept-btn-handler")
cookie.click()
freesim = driver.find_element_by_link_text("Free Sim")
freesim.click()

Note that you're trying to pass multiple class names to find_element_by_class_name (you need to pass one only) and call driver.implicitly_wait(5) several times (you might do this only once)

Your class name contains spaces and thus are actually multiple class names. You can make a CSS selector by prefixing each class name with a dot.

CSS Selector ->> ".product-item.payAsYouGoProductListerItem.clickable"

You also have to wait until the element is clickable before you click. Add this to/change this in your code. The rest of your code works fine.

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

....
....
....

freesim = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".product-item.payAsYouGoProductListerItem.clickable")))
freesim.click()

To click on the element Free Sim you can use either of the following Locator Strategies :

  • Using css_selector :

     driver.find_element_by_css_selector("button#onetrust-accept-btn-handler").click()
  • Using xpath :

     driver.find_element_by_xpath("//a[@href='free-sim-direct']").click()

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     driver.get("https://mobile.lebara.com/gb/en/free-sim") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='free-sim-direct']"))).click()
  • Using XPATH :

     driver.get("https://mobile.lebara.com/gb/en/free-sim") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='free-sim-direct']"))).click()
  • Note : You have to add the following imports:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By 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