Home > OS >  Error when clicking a button using selenium
Error when clicking a button using selenium

Time:11-05

I'm trying to click on a button using the selenium, but I'm getting an error.

This is the error:

enter image description here

this is the fragment of the accessed html page:

enter image description here

This is my code:

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',options=chrome_options)
wd.get("xxxxxx")


button = wd.find_element(By.CLASS_NAME,"VfPpkd-LgbsSe.VfPpkd-LgbsSe-OWXEXe-dgl2Hf.ksBjEc.lKxP2d.LQeN7.x0t5t") 
button.click()

The objective is: When I click on the button using the selenium a popup appears on the screen

CodePudding user response:

This is (mostly) caused by element not being loaded and you're trying to perform click on it before...

Try with this:

WebDriverWait(wd, 2).until(EC.visibility_of_element_located((By.CLASS_NAME, 'VfPpkd-LgbsSe.VfPpkd-LgbsSe-OWXEXe-dgl2Hf.ksBjEc.lKxP2d.LQeN7.x0t5t'))).click()

Don't forget to import WebDriverWait:

from selenium.webdriver.support.ui import WebDriverWait

This way you can avoid assigning value of that button to variable button and button.click

CodePudding user response:

try xpath or css selector maybe work

  • Related