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()
CodePudding user response:
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()
CodePudding user response:
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