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

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.

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: How to get text without clicking on tab?

I'm trying to find text that is located on a specific tab. For some reason the text is available in the html but when I use the command driver.find_element_by_id('hand-graph-title-overview').text without clicking on the 'Overview' tab it returns a empty string. However, when I do click on the 'Overview' tab it will return 'Unpaired OOP' successfully. Does anyone know how to get the text without clicking on the tab?
Images of what I'm trying to scrape.
https://i.stack.imgur.com/9nSc0.png
https://i.stack.imgur.com/MyvDA.png
Maybe try and get the 'value' attribute of the element.
driver.find_element_by_id('hand-graph-title-overview').get_attribute('value')
The answer is no. You can't access the element from the other tab without focusing on it. Retrieval of text is totally based on the presence of the element on the current page. When you click on tab then automatically it gets loaded in the dom and then only you can access the element.

how to click on radio button which is under a expanding tree node in Selenium

->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();

Can not click on element via TestComplete object mapping

I need to click on a web element, within TestComplete, this element looks like this:
I outlined its boundary using heavy-black line.
The problem is, by default, an element is always clicked in its middle point, and for this element above, clicking its middle point produces no results as it does not click this plus sign.
Within TestComplete, this element can not be broken further down into smaller elements.
I can use Selenium in JavaScript to click it, but is there is an other way?
Thanks
Try using x and y coordinates with the Click method.
SmartBear Click Action
I needed to click a button on my website but TestComplete was only able to identify the parent object which had 3 buttons in it. I was able to use x,y coordinates to press the 3 different buttons in parent object.
Record a script where you click the button and look at the function created to get the coordinates you need.

How can I select always the last radio button?

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