0

I am a beginner on Selenium and I am trying first to load the page then click on the button 'Explore Standards Map' that will take me to another page, but the website pops up and quits and the button does not click. Also there are two buttons with the same name & class Name.

chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chromeOptions)

def init_browser():
    driver.get("https://www.standardsmap.org/en/home")
    print('starting_Driver')
    click_butt = driver.find_element(By.XPATH, "//button[contains(text(),' Explore Standards Map ')]")
    click_butt.click()

init_browser()

2 Answers 2

0

I had to deal with the same problem before. For me the solution was to make the program wait before clicking the button, to give the program enough time to load the webpage.

So this might be the solution for you too:

import time # The module necessary to use the sleep function.

chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chromeOptions)

def init_browser():
    driver.get("https://www.standardsmap.org/en/home")
    print('starting_Driver')
    delay = 5 # 5 seconds.
    time.sleep(delay) # Wait 5 seconds. You can adjust the time if it's too fast or too long.
    click_butt = driver.find_element(By.XPATH, "//button[contains(text(),' Explore Standards Map ')]")
    click_butt.click()

init_browser()
Sign up to request clarification or add additional context in comments.

3 Comments

Your solution will probably work, but this is not a righ way to use hardcoded delays, WebDriverWait expected_conditions should be used instead. Also, no need to keep the element in click_butt variable, click can be applied directly.
@Prophet I didn't know about that, thanks. Yes it isn't necessary unless the OP wants to use it for other purposes aswell other than clicking (if possible, im not really familiar with selenium).
It is possible, sometimes we do more actions than a single click, so sometimes we need to keep the web elemennt object
0

You need to wait for the element to become clickable. This is exactly WebDriverWait expected_conditions are for.
The following code works:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

url = "https://www.standardsmap.org/en/home"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),' Explore Standards Map ')]"))).click()

The result is

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.