I am trying to write and read values from radio buttons and checkboxes which id and name values are changing and not fixed. Therefore I cannot use these as identifiers.
Checkout the below image, this shows a list of radio buttons and checkboxes and the inspected element in chrome. Note that the Name and ID underlined in red are not fixed and cannot be used.
Radio Button & Checkbox - Inspect Element from Chrome
Your id is dynamic, you can not use it. There are so many ways to find element.
By.xpath
By.cssselector
By.linktext
By.name
,....
If you want to use selenium you must know at least first two.
visit link
https://devqa.io/selenium-css-selectors/
https://www.guru99.com/xpath-selenium.html
Related
I am trying to check to see if buttons are present on a page. The number of buttons varies depending on the type of account that logs in. The code below returns true, so I know that the xpath is working.
Boolean WorkspacePresent = driver2.findElement(By.xpath("//body/div[2]/div[1]/div[1]/form[1]/section[1]/input[1]")).isEnabled();
However, I need to make sure that the value is also included, since this just checks how many buttons are there. If I wanted to check for a "Workspace" button, could I still use Find By Xpath?
To identify the Workspacebutton button use following xpath.Similar way you can identify other button.
driver2.findElement(By.xpath("//input[#name='Workspacebutton' and value='Workspace']"))
->ABC
. 1
. 2
Here I have select "ABC" first which expands and shows two radio buttons. I have to find this element on a UI page and click on the 2nd radio button.
as for me, to make it simple, I would like to add css class to those radios, so that I can use find_element(css: '.a').click. This is my lazy and yet effective approach.
If both radio buttons belong to same group then they will have same names
You can extract both of them and click on second one
driver.findElements(By.name("common_name_of_radio_button").click();
On a webpage 3 buttons are present and all 3 buttons have same name eg:, 'continue'. In its html no id is present. I need to click on second button.
Also without using xpath locator.
var elements = this.browser.FindElements(By.Name("//*[contains(#name,'continue')]"));
element[0].Click()
The above code clicks the first element of the 3 elements that are returned.
We can also click the element if you have a unique sibling or preceding element.It could help a lot if you provide the original html code.
I have a verification page to test by selenium webdriver automation. On the verification page there are always three questions.
But those questions are selected from a pool of questions and so for a single user different questions might appear every separate time he comes to verification page.
So say there are 20 questions in my pool and there are five options (radio answers) for each question, so there are 100 separate radio buttons and each has its separate id/name in DOM.
I am unable to automate this piece of the webpage.
In order to proceed with my testing, I need to always select the last radio button for each of the three questions.
The last radio always contains either "None" or "never" in the label text and radio text label is clickable.
Also the name locator always starts with "1402248" for each radio button.
I am using Page object model in my projet.
Can someone help me to understand how can I identify each radio webelement?
I am using this:
#FindBy (xpath = "//*[#class='radio']//a[contains(text(),'never') or contains(text(),'None')]")
protected WebElement oVerifyIdentityFirstAnswer;
This CSS selector should find all radio buttons that are last of their type inside a parent element
driver.findElements(By.cssSelector("input[type='radio']:last-of-type"))
If your radio buttons are all encapsuled individually, move the last-of-type to that container element:
driver.findElements(By.cssSelector("a:last-of-type" > input[type='radio']))
This returns a list of WebElement, which you can iterate over and click every entry. I wouldn't use the #FindBy annotation by the way, since it often leads to staleness exceptions.
Try changing your Xpath to:
xpath = "//*[#class='radio']//a[contains(text(),'never') or contains(text(),'None')][last()]"
I have an application which uses Dijit Combobox. What happens is, I click on the cell. It opens up a dropdown. This is getting properly recorded in the IDE. But, when I choose a value from the drop down, IDE is not recording that part.
Here is the firebug screenshot of the combo box that appears on clicking on the cell. As you can see it has two values cm2 and mat1. I need the IDE code to choose either of these values.
I tried something like
click - //div[#id='csi_table_Widget_5']/div[2]/table/tbody/tr/td[11] This works fine for opening the drop down.
click - //div[#id='widget_csi_widget_FilteringSelect_10_dropdown']/ul/li[4]
This does not work for choosing a value from the drop down.
Any help, please?
If you absolutely have to click the value, then I think you need to check the ID you are using. Based on the DOM you should be using //div[#id='csi_widget_FilteringSelect_10_dropdown']... instead of widget_csi_... I'm not sure a selector on widgetid would work in selenium.
Alternatively, if you know what value you will be entering into the combo, you could use selenium.type for all but the last character you need to enter, and then selenium.keyPress for the last one. This will fire dojo's change method and select the value in question, unless there is more than one matching the string provided.