0

As part of an automated task software, I use Selenium with python to use the web. After some advancement I got stuck with a problem that I can't solve at all.

I would like to find a tag with Selenium to be able to select one of these options. It has an ID and a class. So I write:

select = driver.find_element(By.CLASS_NAME, "myselectclassname")
select.select_by_value('1')
select.click()

Unfortunately I get this error every time:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method": "css selector", "selector":".myselectclassname"}
  (Session info: MicrosoftEdge=IP)

I searched all the help on google and they all tell me to put a delay before the element is found (as the search would be too fast before the web page is generated), so I try again another code with ID :

select= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "theIDofmyselect"))

But after the 10s delay, an error is returned because of the time.

Can someone help me? I have python 3.10 and selenium 4.3.0


Update

The code is very loud so here is a summary part :

<table cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td>
                <span class="blockedElement">
                    <select id="myselectid" class="myselectclassname" style="margin-top:3px;">
                        <option title="text_1" value="0" selected="selected">
                            t   e  xt1   
                        </option>
                        <option title="text_2" value="1">
                            t ex  t2
                        </option>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
BeauCloud
  • 11
  • 5
  • 1
    The code is not behaving like you expecting. In general that is a good indication for time to debug! Depending on your IDE (VSCode, PyCharm, Spyder ) you can go over your source code line by line. Clearly selenium cannot find the element you are pointing at. Another suggestion from me can be trying to find the element that is the parent of what you are looking for. For example what is the parent of your element that has ID and Class? You can try to find that first. – Sezai Burak Kantarcı Jul 25 '22 at 07:54
  • Update the question with the relevant text based HTML of the ` – undetected Selenium Jul 25 '22 at 08:01
  • Did it, I found that the div above doesn't work and I can't find them too. It's only working with the body tag – BeauCloud Jul 26 '22 at 12:53

1 Answers1

0

To select the desired option instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.myselectclassname#myselectid")))).select_by_value('1')
    
  • Using XPATH:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='myselectclassname' and @id='myselectid']")))).select_by_value('1')
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352