Home > Net >  How to click on the element with ID "langSelect-EN" using Selenium
How to click on the element with ID "langSelect-EN" using Selenium

Time:01-21

I have this so far

First_prompt = driver.find_element(By.CSS_SELECTOR,"#promptAnchor #prompt #promptContent")

HTML:

The HTML CODE

For whatever reason I can't add "#promptContentChangeLanguage" into First Prompt it gives me an error saying it can't find the value. I don't know what to do here. I have also tried getting the ID directly "langSelect-EN" but the same error occurs. When I just run the code I have everything is fine, but I can't access what I'm trying to. An image is attached to show you what I'm trying to get to. Any help would be greatly apricated. Thank you.

Link: https://orteil.dashnet.org/cookieclicker/

I'm expecting to get the element "langSelect-EN" and click on it with selenium.

CodePudding user response:

I used the SeleniumBase Recorder to generate a script for that:

pip install seleniumbase, then run with python or pytest:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class RecorderTest(BaseCase):
    def test_recording(self):
        self.open("https://orteil.dashnet.org/cookieclicker/")
        self.click("a#changeLanguage")
        self.click("div#langSelect-EN")

It is working for me.

CodePudding user response:

To click on the element with text as English you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://orteil.dashnet.org/cookieclicker/')
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cc_btn.cc_btn_accept_all"))).click()
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.langSelectButton.title#langSelect-EN"))).click()
    
  • Using XPATH:

    driver.get('https://orteil.dashnet.org/cookieclicker/')
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cc_btn cc_btn_accept_all']"))).click()
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='langSelectButton title' and @id='langSelect-EN']"))).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
    
  • Browser Snapshot:

cookie-clicker

  • Related