I am trying to select from a list of 3 buttons, but can't find a way to select them. Below is the HTML I am working with.
<input name="pollQuestion" type="radio" value="SRF">
<font face="arial,sans-serif" size="-1">ChoiceOne</font><br />
<input name="pollQuestion" type="radio" value="COM">
<font face="arial,sans-serif" size="-1">ChoiceTwo</font><br />
<input name="pollQuestion" type="radio" value="MOT">
<font face="arial,sans-serif" size="-1">ChoiceThree</font>
I can find it by using the following code:
for i in browser.find_elements_by_xpath("//*[#type='radio']"):
print i.get_attribute("value")
This outputs: SRF,COM,MOT
But I would like to select ChoiceOne. (To click it) How do I do this?
Use CSS Selector or XPath to select by value attribute directly, then click it.
browser.find_element_by_css_selector("input[type='radio'][value='SRF']").click()
# browser.find_element_by_xpath(".//input[#type='radio' and #value='SRF']").click()
Corrections (but OP should learn how to look up in documentation)
In Python binding, find_elements_by_css doesn't exist, it's called find_elements_by_css_selector. One should be able to look at the exception message and look back into documentation here and figure out why.
Notice the difference between find_element_by_css_selector and find_elements_by_css_selector? The first one finds the first matching element, the second one finds a list, so you need to use [0] to index. Here is the API documentation. The reason why I use the latter, is because I copied your code, which I shouldn't.
Selenium webdriver Radio button click
When i used xpath :
driver.find_element_by_xpath("//input[#id='id_gender2']").click()
radio button not selected
But I used css_selector :
driver.find_element_by_css_selector("input#id_gender1").click()
radio button selected
find_elements_by_css_selector worked for me,
browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click()
First Radio button was not selected for me also. But after inserting Time it works for me.
driver.find_element_by_class_name("login").click()
driver.find_element_by_id("email_create").send_keys("testsel000#gmail.com")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()
Consider that you have a radio button to select either of the two options, "Male or "Female". Then try using the following :- This is in Python (Selenium).
driver.find_element_by_xpath("//label[contains(text(),'Male')]").click()
browser.find_elements_by_xpath(".//input[#type='radio' and #value='SRF']")[0].click
This ended up being the fix. I was getting errors without the [0] there, that a list does not have a click() attribute (even though there was only 1 match). Thanks for the help user1177636!
Related
So, there's a site I'm trying to parse so it can automatically raising my offers every two hours.
The site designed in that way that you have to mark with checkboxes the lots you want to raise.
Somehow in html code the checkbox doesn't have value, instead it looks like this:
I have to click it manually via using
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "idk what to write so it checks it"))).click()
But I really don't know how do I find it so it can be clicked.
<label>
<input type="checkbox" value="613" checked="">
# value - lot id, checked - means the checkbox is marked
<label>
# and non-checked checkbox code looks like this:
<label>
<input type="checkbox" value="613">
<label>
You can't use By.CLASS_NAME here since it has no class.
You can use:
By.CSS_SELECTOR to find by CSS selectors
chbVal = '613' # in case you need be able to change this
(By.CSS_SELECTOR, f'label > input[type="checkbox"][value="{chbVal}"][checked=""]') # for checked
(By.CSS_SELECTOR, f'label > input[type="checkbox"][value="{chbVal}"]:not([checked])') # for unchecked
or By.XPATH to find by Xpath
chbVal = '613' # in case you need be able to change this
(By.XPATH, f'//label/input[#type="checkbox"][#value="{chbVal}"][#checked=""]') # for checked
(By.XPATH, f'//label/input[#type="checkbox"][#value="{chbVal}"][not(#checked="")]') # for unchecked
Note: These are just based on the html snippet you've included - there might by parent elements with better identifiers that you need to include in your path/selector.
Also,
Somehow in html code the checkbox doesn't have value
but in your snippet it does have value...? Anyway, the examples above include value, but you don't have to include them; you can even exclude them with not(...) as shown for checked. (Btw, not(checked)/not(#checked) should exclude elements that have a checked attribute at all, no matter what the value is.)
Basically I want to input invoices in Xero software for my job. The process is very simple, I have some values that I need to input in some slots. I have a big problem however. The xpath is dynamic (changes every time you refresh).
Basically it changes from something like this:
//*[#id="PaidToName_12ddc347c7bc4f5aa84c452f55660690_value"]
To something like this:
//*[#id="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value"]
So the numbers keep on changing.
I have tried a starts-with function however I am pretty sure that there are two XPATHs that starts with PaidToName or end with value, therefore this doesn't seem like a solution as I get this error message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
The other thing to note is that I see many elements that have the "input type hidden" in the HTML code which I am pretty sure play a role with that. Please let me know if there is anything I can do to help.
This is the code I have tried that doesn't work.
button = driver.find_element_by_xpath("//*[starts-with(#id,'PaidToName')]")
button.send_keys('lol')
This is the HTML code I am trying to retrieve
<input type="text" size="24" autocomplete="off" id="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value" name="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value" class="x-form-text x-form-field autocompleter x-form-focus" tabindex="10" style="width: 129px;">
You can use xpath with id and class combination, try this :
button = driver.find_element_by_xpath("//*[contains(#id,'PaidToName') and contains(#class,'x-form-text')]")
button.send_keys('lol')
Try below given locator.
driver.find_element_by_xpath("//input[contains(#id,'PaidToName') AND contains(#class,'x-form-text')]")
I have the following checkbox element:
<input checked="checked" class="iceSelBoolChkbx"
id="mainContentId:newSerchCheckBox" name="mainContentId:newSerchCheckBox"
onblur="setFocus('');"
onclick="var form=formOf(this);iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="var form = formOf(this);
Ice.util.radioCheckboxEnter(form,this,event);"
type="checkbox">
I currently am locating the element and attempting to click it using
check_box = driver.find_element_by_id('mainContentId:newSerchCheckBox')
check_box.click()
When I run the code it runs without any errors but on the site the checkbox is still unchecked. What could be causing this and is their an alternative way to check the box?
Try replacing click with mousedown, I don't know what the underlying difference is but it resolved the issue I was having with click().
driver.execute_script('$("#mainContentId:newSerchCheckBox").trigger("mousedown")')
Let me know if you're thrown an error and we can re-write it in pure Javascript rather than relying on jQuery.
I think your checkbox is already selected if you see the input tag checked="checked".So you need to check first whether selected or not.If selected do not click.
element=driver.find_element_by_id("mainContentId:newSerchCheckBox")
if element.is_selected():
print('Check box is already selected')
else:
element.click()
I have a web page which i am automating, I can click on any other elements using xpath or name or ID, but this gets little tricky when the element which I want to click is inside label. Here is the example,
<div class="dv-widget dv-deco-def dv-sz-med dv-map-sw">
<input name="l_maps" id="grp_perc" value="0" class="map_HD dv-radio-swr" type="radio">
<label for="grp_percentage" class="dv-radio-swl" onclick="">%</label>
<input name="l_maps" id="grp_count" value="1" class="map_HD dv-radio-swr" checked="" type="radio">
<label for="grp_multiply" class="dv-radio-swl" onclick="">*</label></div>
I need to click on the radio buton with the text % on it, I tried several option using xpath and CSS and ID but nothing seems to find that element under that label. I need help on this guys please. Thank you in advance.
This is kind of a strange situation. Typically the for attribute of the LABEL matches the ID of the INPUT that it corresponds to, e.g.
<input id="name" ... >
<label for="name" ... >
But in this case it doesn't match. We can get around it pretty easily. You can search for the element that contains the % text using XPath and then find the preceding INPUT.
//label[.='%']/preceding-sibling::input
If you plan to use this elsewhere also, I would suggest that you put this into a function and pass in the LABEL text, e.g. % and feed that into the locator to click the matching INPUT.
To click on the radio button with the text as % you can use either of the following lines of code :
Using preceding :
driver.find_element_by_xpath("//label[#class='dv-radio-swl' and #for='grp_percentage']//preceding::input[1]").click()
Using preceding-sibling :
driver.find_element_by_xpath("//label[#class='dv-radio-swl' and #for='grp_percentage']//preceding-sibling::input[1]").click()
Using ancestor :
driver.find_element_by_xpath("//label[#class='dv-radio-swl' and #for='grp_percentage']//ancestor::input[1]").click()
Update
As per the first part of the xpath I have provided it correctly identifies the <label> element with text as %. See the snapshot :
Now, appending preceding makes no mistake to identify the previous <input> tag. See snapshot :
So in all means our xpath is correct. Possibly, you need to induce a waiter for the element to be clickable as follows :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#class='dv-radio-swl' and #for='grp_percentage']//preceding::input[1]"))).click()
If you are still unable to locate the element check the HTML if the element is within an <iFrame> tag and you will find a detailed discussion in How can I select a html element no matter what frame it is in in selenium?
I am trying to automate a process of clicking some buttons. Although I have come really far and only 1 button that is not being able to work . I am using python with selenium. So I just want to click this button but I am unable to do so. Below is my code I have tried with css select and by xpath but Still I am unable to click it , I am getting error path not found.
This is the button that I want to click
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit" type="button" onclick=";return false;" data-channel-creation-token="GhaqucG9ARAKDi9teV92aWRlb3M_bz1VKAQ%3D"><span class="yt-uix-button-content">CREATE CHANNEL</span></button>
I have tried the following 2 codes but none of them work.
driver.find_element_by_xpath("//button[#class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()
driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()
Let's go over your attempts:
driver.find_element_by_xpath("//button[#class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()
This one did not work because you are trying to put a CSS selector into a #class attribute value check. You meant to do something like:
//button[#class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']
driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()
This one did not work since you are not specifying multiple classes in a CSS selector correctly, classes need to be separated with a dot:
button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit
Note that a much simpler selector should do the job - you don't have to specify all classes in a CSS selector - pick a more data-oriented and unique one, in this case I think this should be reasonably reliable and readable:
driver.find_element_by_css_selector('button.create-channel-submit').click()
Assuming you want to click on the button CREATE CHANNEL you need to consider the presence of the <span> tag, within the <button> tag. You can use the following line of code:
driver.find_element_by_xpath("//button[#class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']/span[class='yt-uix-button-content']").click()
You can click on your element with next method:
driver.find_element_by_xpath("//span[(#class='yt-uix-button-content') and contains(text(), 'CREATE CHANNEL')]/..").click()
You can try this code
driver.find_element_by_css_selector('button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit').click();
can you try using by_class_name. You are using xpath and css to look up the class name
driver.find_element_by_class_name('yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit')