Home > Blockchain >  Why DOM-element is not displayed to selenium webdriver?
Why DOM-element is not displayed to selenium webdriver?

Time:10-23

I need to press green button in to accept cookies on https://garantex.io/ My code:

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get("https://garantex.io/")
try:
    cookie_access = driver.find_element_by_class_name('btn.btn-success')
    print("Is displayed = "   str(cookie_access.is_displayed()))
except Exception as e:
    driver.close()
    print(e)

I have Is displayed = FALSE. How can I interact with this button?

CodePudding user response:

The button is clicked with me normally, maybe you need to add some wait like this:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

...
driver.get("https://garantex.io/")
cookie_access = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "btn.btn-success")))
cookie_access.click()

You also don't need driver.implicitly_wait(10) at all before loading the page, maybe after it.

CodePudding user response:

You were almost correct. but that is CSS selector not class name.

driver = webdriver.Chrome(ChromeDriverManager().install())
# driver.implicitly_wait(10)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://garantex.io/")
try:
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn.btn-success"))).click()
    print('Clicked on cookies button successfully.')
    #print("Is displayed = "   str(cookie_access.is_displayed()))
except Exception as e:
    driver.close()
    print(e)

You will need these imports as well

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