I'm trying to select a checkbox on the following public webpage using Selenium XPath from Python and click it to change the checked status.
http://simbad.u-strasbg.fr/simbad/sim-fout
For example, the checkbox that I would like to click is located under "Fluxes/Magnitudes" and is named "U".
Upon inspection of this page I built the following XPath to select the checkbox:
//*[#type ='checkbox' and #name='U']
This returns what I believe to be the correct element, however when I try to run click() on the object it fails with the exception 'list' object has no attribute 'click'
When I look at the functions for this object in a debugger it indeed does not have a click function. How can this be true for a checkbox? Is there a different element that has to be selected?
Thanks!
You didn't paste your code here, but from the error you getting it's quite clear, that you are using
driver.find_elements_by_xpath('//*[#type ='checkbox' and #name='U']')
instead of
driver.find_element_by_xpath('//*[#type ='checkbox' and #name='U']')
find_elements_by_xpath method returns a list of web elements. You can apply click() method only on a single element, that's why you should use
driver.find_element_by_xpath('//*[#type ='checkbox' and #name='U']')
or alternatively
driver.find_elements_by_xpath('//*[#type ='checkbox' and #name='U']')[0]
to get the first (single) element from the returned list
Related
I need to find a button element using XPath. The problem is, the button has no unique identifier like Id or name, but is located inside another element which has a unique class (i.e. a class that no other element on page uses). I know how to get to that element and identify it by class but I don't know how to access the button inside that element.
.//mat-header-cell[#class='mat-header-cell cdk-column-delete mat-column-delete ng-star-inserted']
Any suggestions?
SOLUTION I FOUND:
Just added '//button' to the end of xpath and it works now!
.//mat-header-cell[#class='mat-header-cell cdk-column-delete mat-column-delete ng-star-inserted']//button
This one is bugging me. The same xpath is waited for and found. Then i attempt to click it and get an error. Code as below
Wait Until Element Is visible xpath=//*[#id="content"]/div[1]/div/div/div[2]/div[1]/div/h3/a/i[1]
Click Link xpath=//*[#id="content"]/div[1]/div/div/div[2]/div[1]/div/h3/a/i[1]
Error here
ValueError: Element locator 'xpath=//*[#id="content"]/div[1]/div/div/div[2]/div[1]/div/h3/a/i[1]' did not match any elements.
I know for sure by going in manually that the xpath is there. Something funny about the way Robot does this perhaps?
Click link will actually look for a link attribute (a=) but your locator is not an 'a' attribute.
Try 'Click Element' instead of 'Click Link'
I am trying to automate actions and unable to select an element due to its dynamic nature.
I am running Selenium web driver on ruby and am trying to select value that is not present in page source.
<a class="linkOtherBrowser" onclick="addChangeStatusField('InitialSelectionPage');submitFormByAction('ChangeStep');return false;" href="#"><div class="processBarElement noSelected">
<div class="whiteBeforeProcessBarTitles"></div>Initial Selection</div>
<div class="endOfElementOfProcessBar"></div></a>
I am trying to select value "Initial Selection" from above.
Could anyone pls help out?
Thanks,
Abhishek
As the HTML is generated by Javascript, You need to inspect the DOM instead of viewsource and write the element locator code accordingly.
Note: In IE, Firefox or Chrome you can press F12 key to see the developer tools and use the inspect element option to check the DOM.
Whatever element is generated dynamically is added in your DOM. WebDriver has capability of clicking on elements are the visible on UI and hence if the generated element is visible to regular user's you can click on the element easily.
To do so, you need to identify the best selector for that newly generated click, could be xpath or css. Once you identify the selector you can consider clicking clicking using following code
WebElement element = driver.findElement(By.xpath("//a[#title='NAME_TITLE']"));
element.click();
OR
WebElement element = driver.findElement(By.css("a[title='NAME_TITLE']"));
element.click();
There are more options within your By.class on picking the element in best way
I am working on a Drupal project which is using the Editable fields module.
Using that module I'm exposing a dropdown list of text options. It works just great. You click on the list, select an option and the option is updated via Ajax.
My challenge is I'm trying to change the options programmatically via jQuery. Using the following code:
jQuery('select#edit-field-status-0-field-status-und').val(1);
... my browser console area is happy with the code but the Ajax update does not take place.
I tried:
jQuery('select#edit-field-status-0-field-status-und').val(1).change();
Again no errors but the Ajax event still did not execute.
$('#edit-field-status-0-field-status-und').val("1");
will do the trick, as the only reason it wouldn't work would be that you have your select values as strings instead of numbers.
Alternatively the following is more detailed:
$('#edit-field-status-0-field-status-und option').eq(1).prop('selected', true);
Also this is not an 'AJAX' function, it's simply Jquery updating the DOM for the particular element.
The code I was using as recreated below was correct:
jQuery('select#edit-field-status-0-field-status-und').val(1).change();
I found out the reason why it wasn't working was because the ID of the target element changed dynamically.
So when I first inspected and found edit-field-status-0-field-status-und, the same element would change IDs to something like edit-field-status-0-field-status-und--1.
That was throwing things off and gave the impression my code wasn't working.
Thanks to #gts for your input.
I am trying to select a link/button inside of a form, that is in a div. the way it was made is that there are two links/buttons of the same id, name etc. however they are in different forms, so the code i wanted to use is:
_myTest.Form(Find.ById("PermissionsForm")).Child(Find.ByClass("saveBtn")).Child(Find.ByText("SAVE"));
any help would be appreciated
I think this is what you need:
var button = _myTest.Form("PermissionsForm").Button(Find.ByClass("saveBtn"));
This will lookup the button having the class 'saveBtn' inside the form 'permissionsform' in your browser instance _myTest.
Have a look at this page to decide if you need to have .Button(..) or .Link(...) based on the html tag that is used in your html.
How about building a regex for the element?
Regex could something like this
Regex elementRe = new Regex("id=LnkOk href="http://stackoverflow.com");
Then you can use your code to Click this link. The Click method can be extended to accept Regex if it is not already doing that.
Let me know how this goes or if you need more info.
Cheers,
DM