How to select a radio button that changes its id' in selenium? - java

I need some help trying to select a radio button on Selenium IDE.
This radio button value changes randomly, due this I cannot get the radio button selected.

There are multiple ways selenium can locate an element other than referencing the id of an element. Have you also tried using a xpath or css selector? Take a look at the selenium docs for locating an element for examples of using xpath or css selectors.
Post an example of your html and I can help you write the selector.

Use the following commands:
storeAttribute| path of area where radiobutton placed # id| variableName
Now you have dynamic id of that element . Use that value to click on it.
click| ${variableName}

Use a CSS or Xpath selector instead of the default used when you recorded your script.
For example, try this page with radio button examples, scroll down to the Milk/Butter/Cheese example:
The selector for the first radio button ("Milk") could be one of the following:
css = .table5 > input
css = .table5 > input[value='Milk']
A couple of ways you could select the second button would be:
css = .table5 > input ~ input
css = .table5 > input[value='Butter']
You may need to read up on CSS selectors and/or Xpath. You can do a web search for cheat sheets and primers. They are both essential for optimum Selenium scripting, be it with IDE, RC, or Webdriver.
You will find that Firepath is also a great tool to help you build your selectors.

Related

How to click a button where the class is the same for another options?

I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.

Selenium WebDriver "java", I can not click on button on footer

The scenario is:
Try to add experience in linkedin.
Then click on save button to save the added experience.
The below is html code for this button:
<button class="pe-form-footer__action--submit form-submit-action Sans-15px-white-100%" type="submit">
Save
</button>
I am trying to find it by xpath using:
#FindBy (xpath = "//*[contains(text(), 'Save')]")
WebElement saveExperienceButton;
Following screenshot may help:
I will appreciate your help.
if you do not mind css/xpath selectors that do not look very elegant, you can always open up Chrome developer tools on the website you wanna test with Selenium, mark the DOM elements you wanna access and in the context menu choose 'Copy xpath' or 'copy selector':
Try this xpath:
(//*[text()='Save'])[2]
On my profile there are 2 Save buttons - the second one is the skill save. Also, you might want to check this question for the contains syntax.
Creating an XPath using text is a less preferable way. instead of that use other attribute value which is unique.
for ex: in your case
//footer//*[contains(#class, 'form-submit')]

I want to perform click action on button present under "svg" layout using selenium library

I want to perform click action on a button present under .svg layout using Selenium (Java bindings).
For example, I want to click on the button element, but every time I try to find an element by xpath, I get exception `enable to locate element
I read that with Selenium its tricky to click on element present under the .svg.
Is there anybody who knows a solution because, I haven't found a suitable solution on the net by myself.
Find below HTML code look likes this way:
My code:
List <WebElement> frame1=driver.findElements(By.xpath("//iframe[contains(#id,'-06636000002Pb2L')]"));
System.out.println(frame1.size());
System.out.println(frame1.get(0).getAttribute("title"));
driver.switchTo().frame(0);
ElementaryOperations.Sleep(3000);
System.out.println("New relation frame found");
driver.findElement(By.cssSelector("#newrel")).click();
After switching to frame successfully, I am not able to click on the element present under the SVG layout. Please refer to the attached screen shot(link)
this is how you should be able to identify SVG node and then sub nodes of that.
//*[local-name() = 'svg']
use this code
driver.findElement(By.id("rfb")).click();
try using class attribute of the button using CSS Selectors:
driver.findElement(By.cssSelector(".btn.btn-primary")).click()

How to use "And" operator using selenium webdriver with java?

I have a login button and want to identify it using xpath and id. But both the xpath and id are dynamically changing. Please advise on how to do it.
The Xpath and ID keep changing as below for login button and i am not able to find any common element to find it:
Xpath : .//*[#id='uO4Qo']
ID : uO4Qo
Xpath : .//*[#id='tP5Qo']
ID : tP5Qo
Xpath : .//*[#id='vVBPn']
ID : vVBPn
If you're using Selenium IDE to create your script, the target field is a dropdown, displaying all the possible options to identify your specified button. If the options CSS, name or anything else are not available, it means that these things are not present on your page. Perhaps is xpath:position an option in your specific case.
Use below code, use login button text in place of LoginButtonText:
driver.findElement(By.LinkText("LoginButtonText")).click();

Unable to find a button wrapping elements containing specific text for Selenium testing

Given the following Html:
<button type="button"><b><em><u>Next</u></em></b></button>
In the page source I have more than one button. I have only option to find this element with text Next. I tried following methods but without success:
driver.findElement(By.cssSelector("button>b>em>u[text()='Next']"))
driver.findElement(By.cssSelector("button>b>em>u[.='Next']"))
driver.findElement(By.cssSelector("button>b>em>u.contains('Next')"))
It might be the case that the button has whitespace or other characters, I would switch to an xpath selector.
Selecting the u element
This xpath will select the u element:
driver.findElement(By.xpath("//button/b/em/u[contains(., 'Next')]"))
Selecting the button
To select the button containing the above u element, e.g. so that the button can be clicked:
driver.findElement(By.xpath("//button[b/em/u[contains(., 'Next')]]"))
Xsl fiddle of this here
Element not Found?
As an aside, and in general, when looking at HTML to determine css or xpath selectors for Se, ensure that you are looking at the final rendered version of Html, and not just the original Html served from the web server - client side javascript may have modified the html significantly after loading, and also note that if the served html was not well formed, that browsers can also change html. In Chrome, I use the Developer tools : Elements pane to view html.
Try assigning an ID to the button and then map the functionality to the button ID rather than button text like this:
<button id ="btn1" type=button">
You are combining xpath selector syntax in your css selector. Try this in stead:
driver.findElement(By.xpath("//button/b/em/u[text()='Next']"))
or
driver.findElement(By.xpath("//u[text()='Next']")).
If you do wish to use a css selector, remove the text() part like so:
driver.findElement(By.cssSelector("button>b>em>u"))
Below xpath should find 1st occurrence of "Next" button:
//button[text()='Next'][1]
If you want to get 2nd occurrence:
//button[text()='Next'][2]

Categories

Resources