0

In a Selenium test of my homepage I ran into the following problem:

I have a number of buttons on the page. When clicking one of them a selections list is populated with a number of options via a javascript function. After clicking one of the buttons the selection list can look like this:

<select id="selectionList" name="List1" size="10" style="width:100%;">
<option style="color:#0275d8">Item Type 1</option>
<option onclick="onSelection()" id="Item_1">Item 1</option>
<option onclick="onSelection()" id="Item_2">Item 2</option>
<option onclick="onSelection()" id="Item_3">Item 3</option>
<option onclick="onSelection()" id="Item_4">Item 4</option>
<option onclick="onSelection()" id="Item_5">Item 5</option>
<option onclick="onSelection()" id="Item_6">Item 6</option>
<option style="color:#0275d8">Item Type 2</option>
<option onclick="onSelection()" id="Item_7">Item 7</option>
<option onclick="onSelection()" id="Item_8">Item 8</option>
<option onclick="onSelection()" id="Item_9">Item 9</option>
<option onclick="onSelection()" id="Item_10">Item 10</option>
</select>

In my test I used the following to click on one of the items.

WebDriverWait(self.browser, 60).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='Item_1']"))).click()

I do see that item 1 gets highlighted but the onSelection() function is not called.

I also tried this

time.sleep(10)
self.browser.find_element_by_id("Item_1").click()

Again item 1 gets highlighted but the function onSelection() is not called.

Any idea of how to solved this issue?

2 Answers 2

2

this seems to be a known problem

https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/2556

the issue is marked closed but seemingly only because the original reporter abandoned the issue and/or because it is an imported bug reported from an older issue tracker.

there is one reported workaround in the discussion

driver.action.click_and_hold(element).perform
sleep(1)
driver.action.release.perform

i think the code is informal because AFAIK there is no driver.action. instead you have to do actions = ActionChains(driver).

see here for more information: https://www.tutorialspoint.com/what-are-actionchains-class-in-selenium-with-python

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

1 Comment

Thanks for directing me towards ActionChains. I used following solution: ActionChains(self.browser).move_to_element(WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.ID, "Item_1")))).click().perform() It works nicely
0

The element is a element so you need to use the Select() class.

To select the <option> with text as Item 1 using Selenium 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 = Select(WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#selectionList[name='List1']"))))
    select.select_by_visible_text('Item 1')
    
  • Using XPATH:

    select = Select(WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='selectionList' and @name='List1']"))))
    select.select_by_visible_text('Item 1')
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions in:

2 Comments

This import is also needed: from selenium.webdriver.support.ui import Select Your suggestion does not solve the issue. Again the item is selected but the function is not called. I managed to solve the issue using ActionChains, see above.
@Tola Added the import

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.