I'm trying to click
<div onclick="registerAcc()">Register</div>
if you visit krunker.io
and inspect and then just ctrl f Register you should find it.
Here is my code:
driver.find_element(By.CLASS_NAME,"Register").click()
and the error is "no such element"
CodePudding user response:
i have tried to open krunker.io, and found out the register not like what you discribed. as u discribed in your questions, the right code is:
driver.find_element(By.CLASS_NAME,"accBtn button buttonP").click()
compare with my code and your code, you will find out that the class name is just behind "div --no-sandbox") # chrome_options.add_argument("--headless") webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary browser = webdriver.Chrome(service=webdriver_service, options=chrome_options) url = 'https://krunker.io' browser.get(url) WebDriverWait(browser, 200000).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click() print('Accepted terms') WebDriverWait(browser, 200000).until(EC.element_to_be_clickable((By.XPATH, "// div[contains(text(), 'Login or Register')]"))).click() print('clicked login/register') WebDriverWait(browser, 200000).until(EC.element_to_be_clickable((By.CLASS_NAME,"buttonP"))).click() print('clicked Register button')
CodePudding user response:
The Krunker website uses AJAX calls.
Solution
To click on the element Register you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
driver.execute("get", {'url': 'https://krunker.io/'})
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='signedOutHeaderBar' and contains(., 'Login or Register')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='accBtn button buttonP' and text()='Register']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC