I'm trying to click on an element (radio button) using Selenium (in Python), I can't disclose the URL because it's a private corporate intranet, but will share the relevants part of code.
So basically this element is within an iframe, thus, I've used the following code to get the element:
# Select the item on main DOM that will udpate the iframe contents
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='sm20']"))).click()
# Don't sleep, but only WedDriverWait...
wait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='ifrInterior']")))
# Select the element inside the iframe and click
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo']"))).click()
The HTML code for the element is this:
<span class="W1">
<input type="radio" name="gestionesPropias_Equipo" value="1" onclick="javascript:indicadorPropiasEquipo(); ocultarPaginacion('1'); limpiarDatosCapaResultado();">
</span>
I'm interested in clicking this because when we click on it a drop-down is enabled:
If clicked then the dropdown is enabled:
The intersting HTML code for this is
<span id="filtroAsignado" class="W30">
<select name="nuumaAsignado" class="W84">
<option value="">[Todo mi equipo]</option></select>
</span>
Debugging a bit Selenium I can see that the elemtn is found:
And this is actually the base64 image of the element, which is the expected radio button
So I'm wondering why the element actually does not get clicked??
UPDATE: Based on request from #DebanjanB, I'm adding the HTML code from the iframe, which is enclosed inside a div in the main page:
<div id="contenido">
<iframe frameborder="0" id="ifrInterior" name="ifrInterior" src="Seguimiento.do?metodo=inicio" scrolling="auto" frameborder="0"></iframe>
</div>
Actually if I look for the word "iframe", there's only one...
Now checking the iframe source itself, has several iframes hidden but the element I need to interact with is in the iframe mentioned above, the only thing that I forgot to mention is that it's inside a form, but I guess that's not relevant? You can see the whole structure in the following image:
Great question. If you know that selenium found the element, you can use Javascript to click the element directly.
The syntax is:
driver.execute_script("arguments[0].click();", element)
You can also do this, which works the same way:
automate.execute_script("arguments[0].click();", wait.until(EC.element_to_be_clickable((By.XPATH, 'Your xpath here'))))
Essentially you are having Selenium run a javascript click on the element you have found which bypasses Selenium. Let me know if this helps!
I don't see any such issue with your code block either. Perhaps you can try out either of the following options:
Using ActionChains:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo' and #type='radio']")))).click().perform()
Using executeScript() method:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo' and #type='radio']"))))
Related
Here is the HTML Code:
<div class="ui-helper-hidden-accessible">
<input type="radio" name="group2" value="yes">
</div>
Full script
Here are the code which i've tried to click the radio button:
driver.find_element(By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]').click()
AND
rdbutton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]'))).click()
AND
rdbutton = driver.find_elements(By.XPATH, "//input[#name='group2']")
for rdbuttons in rdbutton:
if rdbuttons.is_selected():
pass
else:
rdbuttons.click()
It seems that you are using wrong locator. From the HTML you have given i think you are trying to click on radio button that have label Ya if this is the case then try with below xpath:
"//label[contains(text(),'Ya')]"
OR
"//*[normalize-space()='Ya']"
If this does not solve your problem, so please explain and add HTML of proper element either by screenshot or in textual format
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
Sorry guys, I asked the same question before but I can't apply in my real task. (please see previous quest tion here)
I got 2 trouble:
1/ project based on SeleniumLibrary and I don't know how to call find_element_by_xpath or something else in common (import webdriver)
2/ with these code below, the SeleniumLibrary tell me that is not valid XPATH expression.
driver = SeleniumLibrary
driver.open_browser("abc.com")
driver.find_element("//div[#id='react-select-2--value']").click()
driver.find_element("//[test()='Core']) --> Error here "str... is invalid XPATH Expression"
3/ in previous question, I solved the problem (as people's answers)
1.
driver.get("https://react-select.com/home")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.basic-single > div.select__control > div.select__value-container"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class, 'select__menu')]/div[contains(#class, 'select__menu-list')]//div[contains(#class, 'select__option') and text()='Green']"))).click()
or:
driver.get("https://react-select.com/home")
driver.maximize_window()
driver.find_element_by_xpath("//div[#class='select__value-container select__value-container--has-value css-1hwfws3']").click()
driver.find_element_by_xpath("//*[text()='Green']").click()
# add span tag
<span class="Select-value-label resourceValue" style="margin-left: 5px;" xpath="1">Core</span>
driver.find_element("//[test()='Core'])
you have not provided the tag
driver.find_element("//*[test()='Core']")
you can privide * indicating any tag or specify which tag input,div etc
Also see the elements are in iframe or not , if its inside iframe then you have to switch to it before you could interact with it.
iframe = driver.find_element_by_xpath("//iframe")
driver.switch_to_frame(iframe)
driver.find_element("//div[#id='react-select-2--value']").click()
driver.find_element("//*[test()='Core']")
Abd if you have to interact with element outside iframe then, then you have to switch out from iframe first:
driver.switch_to_default_content()
//rest of code
I don't understand why when I go to this page
https://www.ryanair.com/it/it/voli-low-cost/?from=BGY&out-from-date=2018-09-01&out-to-date=2018-09-30&budget=60&trip-length-from=1&trip-length-to=3
or whatever page from that website, Selenium doesn't allow me on the script to run the next page button with the code:
next_page = browser.find_element_by_xpath('/html/body/div[2]/main/div/div[2]/farefinder-widget/div/div[2]/div[2]/div/div/core-pagination/div/div[2]/a')
next_page.click()
On the HTML code:
<a ng-class="{'disabled': $ctrl.isLastPage($ctrl.currentPageIndex)}" ng-click="$ctrl.handleNextClick()" class="core-link"><span class="nav-label">Successivo</span><core-icon icon-id="glyphs.chevron-right" class="icon-22 fill-ryanair-bright-blue"><div><svg tabindex="-1" focusable="false" role="img"><use xlink:href="/it/it/voli-low-cost/?from=BGY&out-from-date=2018-09-01&out-to-date=2018-09-30&budget=60&trip-length-from=1&trip-length-to=3#glyphs.chevron-right" ng-href="/it/it/voli-low-cost/?from=BGY&out-from-date=2018-09-01&out-to-date=2018-09-30&budget=60&trip-length-from=1&trip-length-to=3#glyphs.chevron-right"></use></svg></div></core-icon></a>
Whereas If I write it on the shell, oddly enough it does work, with the same exact lines. I've also tried to give it some seconds with time.sleep, but nothing happened.
Pagination appears to be disabled for this element, i.e. <a ng-class="{'disabled': $ctrl.isLastPage..... It works for me if I click on the parent div, like so:
browser.find_element_by_css_selector(".next.vertical-center").click()
Or, if you want use your xpath, you just need to remove the last 'a':
next_step = browser.find_element_by_xpath('/html/body/div[2]/main/div/div[2]/farefinder-widget/div/div[2]/div[2]/div/div/core-pagination/div/div[2]')
For readability, I recommend the CSS selector.
EDIT: You are probably getting an exception because the element is not visible. Just need to scroll the element into view.
element = browser.find_element_by_css_selector(".next.vertical-center")
browser.execute_script("arguments[0].scrollIntoView();", element)
element.click()
I got stuck with extracting href="/ttt/play" from the following HTML code.
<div class="collection-list-wrapper-2 w-dyn-list">
<div class="w-dyn-items">
<div typeof="ListItem" class="collection-item-2 w-clearfix w-dyn-item">
<div class="div-block-38 w-hidden-medium w-hidden-small w-hidden-tiny"><img src="https://global-uploads.webflow.com/59cf_home.svg" width="16" height="16" alt="Official Link" class="image-28">
<a property="url" href="/ttt/play" class="link-block-4 w-inline-block">
<div class="row-7 w-row"><div class="column-10 w-col w-col-2"><img height="25" property="image" src="https://global-fb0edc0001b4b11d/5a77ba9773fd490001ddaaaa_play.png" alt="Play" class="image-23"><h2 property="name" class="heading-34">Play</h2><div style="background-color:#d4af37;color:white" class="text-block-28">GOLD LEVEL</div><div class="text-block-30">HOT</div><div style="background-color:#d4af37;color:white" class="text-block-28 w-condition-invisible">SILVER LEVEL</div></div></div></a>
</div>
<div typeof="ListItem" class="collection-item-2 w-clearfix w-dyn-item">
This is my code in Python:
driver = webdriver.PhantomJS()
driver.implicitly_wait(20)
driver.set_window_size(1120, 550)
driver.get(website_url)
tag = driver.find_elements_by_class_name("w-dyn-item")[0]
tag.find_element_by_tag_name("a").click()
url = driver.current_url
print(url)
driver.quit()
When I print url using print(url), I want to see url equal to website_url/ttt/play, but instead of it I get website_url.
It looks like the click event does not work and the new link is not really opened.
When using .click() it must be "visible" (you using PhantomJS) and not hidden, in a drop-down for example.
Also make sure the page is completely loaded.
As i see it you have two options:
Ether use selenium to revile it, and then click.
Use java script to do the actual click
I strongly suggest to click with javascript, its much faster and more reliable.
Here is a little wrapper to make things easier:
def execute_script(driver, xpath):
""" wrapper for selenium driver execute_script
:param driver: selenium driver
:param xpath: (str) xpath to the element
:return: execute_script result
"""
execute_string = "window.document.evaluate('{}', document, null, 9, null).singleNodeValue.click();".format(xpath)
return driver.execute_script(execute_string)
The wrapper basically implement this technique to click on elements with javascript.
then in your selenium script use the wrapper like so:
execute_script(driver, element_xpath)
you can also make it more general to not only do clicks, but scrolls and other magic..
ps. in my example i use xpath, but you can also use css_path basically, what-ever runs in javascript.