How can I select always the last radio button? - selenium

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()]"

Related

Selenium RadioButton & Checkbox button values without ID or Name

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

selenium, hidden button by overlapped input ( not clickable)

Is there any way on how to click a hidden button by an overlapped input text field, for example if you go to www.google.com and enter a text to search for, selenium can not find the "Search Google" button because it is hidden by the autocomplete of the text field.
Thanks.
You can bypass the validation by using #click! instead of #click. Basically, this triggers the click via JavaScript rather than through standard Selenium commands.
browser = Watir::Browser.new
browser.goto('www.google.com')
browser.text_field(name: 'q').set('watir')
browser.button(name: 'btnK').click!
If you are only using Selenium, you can do:
btn = driver.find_element(name: 'btnK')
driver.execute_script('arguments[0].click();', btn)
As discussed in the comments, you could also close the suggestions box before trying to click the button. You can do this by moving focus to any other element - eg the first link on the page. Depending on what you're testing, this may or may not have value.
browser.text_field(name: 'q').set('watir')
browser.link.focus # move focus to any other element so suggestions close
browser.button(name: 'btnK').click

Finding buttons on a webpage using Selenium

I am trying to automate a web based application and I am having trouble finding buttons in order to click on them.
Basically there are a number of text boxes on a screen and next to each text box is a "Go" button. Once you hit the "Go" a popup will be overlayed on the screen saying "Are you Sure?" and 2 buttons Yes/No. Where I am getting stuck is after the first set of buttons on the Yes/No popup buttons. Each button has a non-unique class name and no name variable, so I am finding each button by its tagName and then clicking on the relevant element
WebElement aButtons = driver.findElements(By.tagName("button");
aButtons .get(x).click();
This works until aButtons .get(x).click(); throws an exception saying the element isn't clickable. On further inspection of aButtons it has a size of 78 elements, despite there being only 33 visable buttons on the page.
Does anyone have a suggestion on how to find my buttons better? Or perhaps how to handle the exception such that I can, with a bit of trial and error, find the element? If the exception could be ignored, I could probably use a for loop in increments of 10 in order to find which element the Yes/No buttons correspond to.
If I ruled the world I would go to the developer of this webpage and ask them to make the buttons more easily identifiable, but that unfortunately is out of the question
When I inspect the elements in question this is the block in the html that gets highlighted
"Go" button:
<
button type "submit" ng-click="vm.click()" ng-disabled = "vm.isDisabled()"
class = "btn btn"> Generate <button/
>
<
button class ="btn" ng-show="vm.runScripts" ng-click="vm.runScript()" ng-
disabled ="vm.runScript.isScriptRunning">
>span class = "glyphicon play" aria-hidden="true"></span>
Yes
</button>
I was having trouble with the formatting of the above since I am unfamiliar with posting html code.v the 2nd button is the yes button.
(Note the "No" button has the same "btn" class name). When I tried to find the element based off of the class name " glyphicon play" I got an error saying compound class names are not valid. I was able to find the "Go" buttons since they were sequential, so like aButtons .get(1).click(); was the first Go button on the page, aButtons .get(2).click(); was the second etc.
I cannot share the url since it is an internal site.

How to click on specific button on a page using selenium, when only button name is present in html?

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.

How to assert a button's property

I am using selenium web driver and currently automating a registration form. There are various fields like username,password etc which we have to fill in the details and click on the terms and conditions check box and the account create button gets enabled.
Right now there is some issue with the java script and all my assertions for the create account button is failing. Is there any way I can work on this issue?
possible solution:
apply getAttribute() function to the element you need. And compare data obtained with expected data to be:
WebElement button = driver.findElement(By.xpath(..blablblalb...));
String color= button.getAttribute("color");
//verifyinh that color is that of expected
Assert.assertTrue(color.contains("red"));