VASILIY
VASILIY

Reputation: 1

implicitly_wait doesn't work python selenium

i got problem - click on button 'add to the bag' doesn't happen when using implicitly_wait. If i use time.sleep, all works ok, but time.sleep is bad method. So, what i need? Get URl, click on device, click on 'add to the bag' button. Yes, i know about https://selenium-python.readthedocs.io/waits.html and https://www.selenium.dev/documentation/webdriver/waits/ but it not help for me. Please help me)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import WebDriverException
import time
try:
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://www.oumua.me/shop")# Get URL
    browser.implicitly_wait(10)
    browser.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div/div[1]/div[1]').click() #Click on device button
    browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]').click() #Click Add to the bag that doesn't working

Upvotes: 0

Views: 593

Answers (4)

Alex
Alex

Reputation: 33

I tried to run it too, it doesn't work for me. It seems that somehow the browser.page_source does not have time to update. I think it's better to use explicit waits as @data_sc did.

P.S. Here it works, but it's not the best solution:

from time import sleep

driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div/div[1]/div[1]').click()
sleep(1)
driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]').click()

Upvotes: 0

data_sc
data_sc

Reputation: 457

Maybe wait for it to be clickable?

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

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "'/html/body/div[1]/div[2]/div[3]/div[1]")))

element.click()

Upvotes: 1

Akzy
Akzy

Reputation: 1926

I check and it's working for me without adding the implicit or explicit wait, in case you do not want to use waits, you can try the below way to click on this button

element = driver.findElement(By.CSS_SELECTOR(".styles__Button-sc-1fxagfa-17.styles__AddButton-sc-1fxagfa-18.bZGRJp.TZGpT"))

driver.execute_script("arguments[0].click();", element)

Upvotes: 0

javier Piña
javier Piña

Reputation: 54

I can try to use javascript.

js = 'document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();'
driver.execute_script(js)

Upvotes: 0

Related Questions