Unable to Click on Dropdown using Python Selenium - python

I'm trying to select an element from a dropdown but getting an error.
Here is the HTML:
<select id="childContextDDL" data-filter="contains" data-role="dropdownlist" data-template="dcf-context-picker" data-value-field="Value" data-text-field="DisplayText" data-bind="events: { change: childContextListChange }, source: childContextList.ChildContextList" style="display: none;">
<option value="1">NATION</option>
<option value="12">ATLANTIC</option>
<option value="16">CHARLOTTE, NC</option>
And this is the code that I'm trying to run:
mySelect = Select(driver.find_element_by_id("childContextDDL"))
print('MySelect is: ',mySelect)
mySelect.select_by_visible_text('ATLANTIC')
I'm getting this error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
What's the possible reason for getting this error? I'm very new to Selenium.
I also want to click on that element after selecting it.

The problem was that the style within the html was set to none. So I had to first change that style to block to make it visible and then proceed with the clicking operation.
Here's the code that worked:
driver.execute_script("document.getElementById('childContextDDL').style.display = 'block';")
mySelect = Select(driver.find_element_by_id("childContextDDL"))
print('MySelect is: ',mySelect)
mySelect.select_by_visible_text('ATLANTIC')
randomClick = driver.find_element_by_id('dcf-user-info')
randomClick.click()

I guess the mySelect element (the dropdown menu) is not visible.
So please try the following:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
mySelect = Select(driver.find_element_by_id("childContextDDL"))
actions.move_to_element(mySelect).perform()
mySelect.select_by_visible_text('ATLANTIC')
If the above doesn't work (I can't see the actual site you are working on), the following can work instead in the element has to be tapped to become enabled
action = TouchActions(driver)
action.tap(mySelect).perform()
mySelect.select_by_visible_text('ATLANTIC')

If it says it is not currently visible, you should try pausing the code with one of this options:
time.sleep(1) #This states to your code to stop for 1 second and the continue with the work.
WebDriverWait(driver, 10).until(EC.element_to_be_visible(BY.value, "12")) # tells the driver to wait a maximum of ten seconds until the value "12" is visible for the driver.

Related

how to change select value use python selenium

I want to get selected option using Selenium WebDriver with Python
but I used select_by_value can't get vaule
I have the following HTML code
<select id="play_date" name="play_date" onclick="javascript:GoodsInfo.GetPlayDate(this);" onmouseover="javascript:GoodsInfo.GetPlayDate(this);" onchange="javascript:GoodsInfo.GetPlayTime(this.value);">
<option value="" selected="">Select Date</option>
<option value="20230202" style="color: black;">Thu, Feb 02, 2023</option></select>
I am trying to get a list of the option values ('20230202') using Selenium.
At the moment I have the following Python code
select_date = Select(wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='play_date']"))))
select_date.select_by_value('20230202')
but it's always have is
selenium.common.exceptions.NoSuchElementException: Message: Could not locate element with index 1
Any help or pointers is appreciated, thank you!
If you get error NoSuchElementException it means that you are using a wrong xpath/css_selector or that the element is inside an iframe. You can check by looking at the HTML
So your case is the latter, and to make the xpath work you have first to switch to the iframe, using driver.switch_to.frame() or better EC.frame_to_be_available_and_switch_to_it.
WebDriverWait(driver, 9).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "product_detail_area")))
dropdown_date = WebDriverWait(driver,9).until(EC.element_to_be_clickable((By.ID, "play_date")))
options = []
# open menu so that options are loaded
dropdown_date.click()
# wait until options are loaded
while not options:
options = driver.find_elements(By.CSS_SELECTOR, "#play_date option:not([value=''])")
time.sleep(1)
# close menu
dropdown_date.click()
print('available options:')
for opt in options:
print(opt.get_attribute('value'))
select_date = Select(dropdown_date)
select_date.select_by_value(input('enter value: '))

Python Selenium - Error: Element not Interactable. Interactive with website pop up window

I'm running a web scraper on my company's website so I can create elements every month. In testing, I have everything working until I get to interact with a pop up menu.
Code that interacts with pop up:
driver.get("www.website.com")
driver.find_element(By.ID, "ButtonCreatePeriod").click()
time.sleep(1)
driver.find_element(By.ID, "NoExpiration").click()
time.sleep(1)
driver.find_element_by_css_selector('[class="k-widget k-dropdown shorterDropDown"]').click()
time.sleep(3)
ddelement2 = driver.find_element_by_xpath("//*[text()='December 2021']")
action2 = ActionChains(driver)
action2.click(on_element=ddelement2).perform()
On action2.click(on_element=ddelement2).perform() I am getting the error:
"Message: element not interactable: [object HTMLLIElement] has no size and location"
I'm guessing this has to do with interacting with the popup.
Within the pop up that is opened with I click "ButtonCreatePeriod" I am opening the drop down menu defined as class="k-widget k-dropdown shorterDropDown".Within this drop down, I need to select the option with the text "December 2021". I cannot use IDs or numerical values here since this varies across other UIs in the website. The options shown on the dropdown are scrollable which could be an issue too.
I used this same code on another UI on the website with no issues but that one did not present a pop up, although it was scrollable.
Any ideas as to how I could have it select "December 2021" here?
Error generated per #cruisepandey suggestion:
Message: element click intercepted: Element <span title="" class="k widget
k-dropdown shorterDropDown k-state-disabled ic-dropdown-readonly"
unselectable="on" role="listbox" aria-haspopup="listbox" aria-
expanded="false" tabindex="0" aria-
owns="MfrCoreTermEffectivePeriodKey_listbox" aria-live="polite" aria-
disabled="false" aria-readonly="true" aria-busy="false" aria-
activedescendant="l581310d-64f8-4bbc-9354-71b6f041d0e3" style="">...
</span> is not clickable at point (434, 383). Other element would
receive the click: <p>...</p>
(Session info: chrome=93.0.4577.82)`
Solution
I basically had to dig deeper into the HTML code and use xpath to further define the lists within the drop down.
driver.find_element_by_css_selector('[class="k-widget k-dropdown shorterDropDown"]').click()
time.sleep(1)
driver.find_element_by_xpath("//div[#id='MfrCoreTermExpirationPeriodKey-list']//li[text()='December 2021']").click()
Possible reasons could be
Screen is not maximized, if so please maximize it when you launch the URL.
driver.maximize_window()
ActionChain may help.
time.sleep(5)
ActionChains(driver).move_to_element(driver.find_element(By.CSS_SELECTOR, ".k-widget.k-dropdown.shorterDropDown")).click().perform()
JS intervention with scrollInto view may help.
time.sleep(5)
ele = driver.find_element(By.CSS_SELECTOR, ".k-widget.k-dropdown.shorterDropDown")
driver.execute_script("arguments[0].scrollIntoView(true);", ele)
Imports :
from selenium.webdriver.common.action_chains import ActionChains

Python Selenium selecting option using Select (element not visible?)

I've tried to use a few approaches mentioned in similar questions with no luck. In the HTML source code there is clearly 'value' and 'text' properties, but I cannot seem to access those when I use selenium.webdriver to access these?
Note selection lead to change in data on the page...
Edit2:
Guy has pointed out below that the actual drop down maybe a element instead of the element. However using el.click() simply flickers and does not open the drop-down.
EDIT1:
The and elements can now be identified but I am unable to make an selection. I believe the page is also in javascript and hence I'm unsure if this affects methods used.
Original post:
Webpage:
https://www.racv.com.au/on-the-road/driving-maintenance/fuel-prices.html
HTML code of selection, for visibility, some options omitted:
<select name="filter-select-6" id="filter-select-6" class="js-dropdown js-select-map js-filter-select" data-filter="#filter-list-60 .js-tab-item" data-url="/bin/racv/fuelprice" style="display: none;" data-parsley-id="3">
<option value="11" data-index="0">LRP</option>
<option value="2" selected="true" data-index="0">Unleaded</option>
<option value="3" data-index="0">Diesel</option>
<option value="8" data-index="0">Premium Unleaded 98</option>
</select>
I believe I can select the Select element without problem:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
url = 'https://www.racv.com.au/on-the-road/driving-maintenance/fuel-prices.html'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(20)
fuel_select = Select(driver.find_element_by_id('filter-select-6'))
When I print the options, I get:
for fuel_option in fuel_select.options:
print(fuel_option)
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-2")>
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-3")>
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-8")>
<selenium.webdriver.remote.webelement.WebElement (session="9a10aa750fa59f4412e0bea4d7aae990", element="0.5927271524692566-11")>
Using select():
for fuel_option in fuel_select.find_elements_by_tag_name('option'):
if fuel_option.text == "Diesel":
fuel_option.select()
Error:
Traceback (most recent call last):
File "C:/file.py", line 18, in <module>
fuel_option.Select()
AttributeError: 'WebElement' object has no attribute 'select'
Using click() or using any of the select_by_xxx():
for fuel_option in fuel_select.find_elements_by_tag_name('option'):
if fuel_option.text == "Diesel":
fuel_option.click()
#or using select_by_xxx
fuel_select.select_by_value('8')
Error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable: Element is not currently visible and may not be manipulated
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.17134 x86_64)
Select is a wrapper around the WebElement, select() is not a valid method. Please refer to Select doc
Have you tried using select_by_value:
fuel_select = Select(driver.find_element_by_id('filter-select-6'))
fuel_select.select_by_value("8")
Or by visible text:
fuel_select = Select(driver.find_element_by_id('filter-select-6'))
fuel_select.select_by_visible_text("Premium Unleaded 98")
EDIT1
Try to click() first to make the dropdown visible:
el = driver.find_element_by_id('filter-select-6')
el.click()
fuel_select = Select(el)
EDIT2:
I believe your issue is more related to the fact that you use the css property style="display: none;"
You should not be able to see the dropdown manually either.
Please refer to the css syntax doc for more detail
When using None: The element is completely removed
Might not be "ideal" but you can change the value of this property to make it visible again using:
driver.execute_script('arguments[0].style.display = "block";', el)
Code would then look like:
el = driver.find_element_by_id('filter-select-6')
driver.execute_script('arguments[0].style.display = "block";', el)
fuel_select = Select(el)
fuel_select.select_by_value("8")
EDIT3:
Just noticed you provided the website! Very useful.
So the dropdown is hidden being another element and only made available once clicked.
Here's the code and it worked for me
from selenium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
from selenium.webdriver.support.ui import Select
# Get the first element and tap on it, note you might have to tap few time.
el = driver.find_element_by_css_selector('.chosen-single > div')
action = TouchActions(driver)
action.tap(el).perform()
# once the dropdown is open it does not seems that the Select el is the one to use
els = driver.find_elements_by_css_selector('.active-result')
for el in els:
if el.text == 'Diesel':
el.click()
break

Perform TAB action until active element is the required element - Python

I want to perform TAB action until I have reached a particular web-element. Until the active element is the below mentioned element, TAB action has to be performed.
>name = driver.find_element_by_name("name")
>name.send_keys("ABC")
>group = driver.find_element_by_name("group")
>group.send_keys("DEF")
I am able to find element till the above state. After that, I want to perform TAB action until the below mentioned element is found. I guess using a loop would help.
elem = driver.find_element_by_css_selector('.PeriodCell input')
Please find below the HTML code
<div class="PeriodCell" style="left:px; width:112px;">
<div class="Effort forecasting">
<div class="entity field-value-copy-selected">
<input type="text" value="0.0" data-start="2014-09-20">
</div>
</div>
<div class="Effort unmet zero" title="">0.0
</div>
</div>
Please help. Thanks in Advance.
You can bring the element to the visible part of the screen by using one of the following methods.
Using driver.execute_script("arguments[0].scrollIntoView();", element)
you can read more about scrollIntoView() method here.
Using the Actions class of selenium webdriver.
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_css_selector('.PeriodCell input')
actions = ActionChains(driver)
actions.move_to_element(element).perform()
You can read the difference between these two methods here
If you still need to use the TAB action to reach the element
from selenium.webdriver.common.keys import Keys
and using .send_keys(Keys.TAB) send the TAB key to the element
To perform TAB Action until you find a particular WebElement would not be as per the best practices. As per your comment the element is hidden so you need to bring the element within the Viewport first and then invoke click()/send_keys() as follows:
myElement = driver.find_element_by_xpath("//div[#class='PeriodCell']//input[#type='text'][#value=\"0.0\"]")
driver.execute_script("return arguments[0].scrollIntoView(true);", myElement)
# perfrom any action on the element
However the alternative using the TAB Action is as follows:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
global element
element = driver.find_element_by_name("name")
element.send_keys("ABC")
element = driver.find_element_by_name("group")
element.send_keys("DEF")
while True:
element.send_keys(Keys.TAB)
element = driver.switch_to_active_element()
if (element.get_attribute("type")=='text' and element.get_attribute("value")=='0.0' and element.get_attribute("data-start")=='2014-09-20'):
print("Element found")
break
# do anythin with the element

Python+Selenium, can't click the 'button' wrapped by span

I am new to selenium here. I am trying to use selenium to click a 'more' button to expand the review section everytime after refreshing the page.
The website is TripAdvisor. The logic of more button is, as long as you click on the first more button, it will automatically expand all the review sections for you. In other words, you just need to click on the first 'more' button.
All buttons have a similar class name. An example is like taLnk.hvrIE6.tr415411081.moreLink.ulBlueLinks. Only the numbers part changes everytime.
The full element look like this:
<span class="taLnk hvrIE6 tr413756996 moreLink ulBlueLinks" onclick=" var options = {
flow: 'CORE_COMBINED',
pid: 39415,
onSuccess: function() { ta.util.cookie.setPIDCookie(2247); ta.call('ta.servlet.Reviews.expandReviews', {type: 'dummy'}, ta.id('review_413756996'), 'review_413756996', '1', 2247);; window.location.hash = 'review_413756996'; }
};
ta.call('ta.registration.RegOverlay.show', {type: 'dummy'}, ta.id('review_413756996'), options);
return false;
">
More </span>
I have tried several ways to get the button click. But since it is an onclick event wrapped by span, I can't successfully get it clicked.
My last version looks like this:
driver = webdriver.Firefox()
driver.get(newurl)
page_source = driver.page_source
soup = BeautifulSoup(page_source)
moreID = soup.find("span", class_=re.compile(r'.*\bmoreLink\b.*'))['class']
moreID = '.'.join(moreID[0:(len(moreID)+1)])
moreButton = 'span.' + moreID
button = driver.find_element_by_css_selector(moreButton)
button.click()
time.sleep(10)
However, I keep getting the error message like this:
WebDriverException: Message: Element is not clickable at point (318.5,
7.100006103515625). Other element would receive the click....
Can you advise me on how to fix the problem? Any help will be appreciated!
WebDriverException: Message: Element is not clickable at point (318.5, 7.100006103515625). Other element would receive the click....
This error to be occur when element is not in the view port and selenium couldn't click due to some other overlay element on it. In this case you should try one of these following solution :-
You can try using ActionChains to reach that element before click as below :-
from selenium.webdriver.common.action_chains import ActionChains
button = driver.find_element_by_css_selector(moreButton)
ActionChains(button).move_to_element(element).click().perform()
You can try using execute_script() to reach that element before click as :-
driver.execute_script("arguments[0].scrollIntoView(true)", button)
button.click()
You can try using JavaScript::click() with execute_script() but this JavaScript::click() defeats the purpose of the test. First because it doesn't generate all the events like a real click (focus, blur, mousedown, mouseup...) and second because it doesn't guarantee that a real user can interact with the element. But to get rid from this issues you can consider it as an alternate solution.
driver.execute_script("arguments[0].click()", button)
Note:- Before using these options make sure you're trying to interact with correct element using with correct locator, otherwise WebElement.click() would work well after wait until element visible and clickable using WebDriverWait.
Try using an ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
# Your existing code here
# Minus the `button.click()` line
ActionChains(driver).move_to_element(button).cli‌​ck().perform()
I have used this technique when I need to click on a <div> or a <span> element, rather than an actual button or link.

Categories

Resources