Unable to derive Xpath syntax for radio buttons without any distinguished value inside div tag - selenium

I want to click a radio button out of 10 radio buttons on a webpage but each radio button tag is exactly same inside Div/INPUT tag, i.e for each radio button values are exactly same and hence not able to derive a xpath to click on it. Can't use contains text to levarage radio button name as radio button name is in different SPAN tag so can't use that as a reference, Please help me: below is the code :
<div class="classname">
<input name="category.value" type="radio" class="classname">
</input>
<span class="classname">Radio button Name</span>

driver.find_element_by_xpath("//span[#class='classname']/input[#value=1]").click()
Your HTML must have some kind of return type, maybe value or name. I belive you can use that.

Mentioned in the comments can you please try:
//span[text()='Radio button Name']/preceding-sibling::input[1]
Not that this is case sensitive.
This works based on the html provided:
If it doesn't work for you can you please verify your HTML. There are lots of other ways of getting the element. If you need to modify the html just do it and let us know :-)

Related

How to find webelement when there is no ID in Selenium test script

I have a button displayed on the browser with the following tag which doesn't have ID or a single CSS class name. How can I find the Webelement and add a click to this button.
<button aria-describedby="notes" aria-label="Notes" autofocus="" class="btn btn-primary" role="dialog" type="button">OK</button>
Web elements can be located by XPath or CSS Selectors based on any unique combination of any attribute values or / and tag names and also based on their parent or child elements.
Since you didn't share the entire HTML of that page we can only guess about the correct unique locator for that element.
With XPath it can be something like:
"//button[text()='OK']"
Or
"//button[#aria-label='Notes']"
In CSS Selector style it will be
"button[aria-label='Notes']"
Consider locating the Element by Tag Name. Go to the parent element which has a id or class name and get subelements by tag name (parentelement.find_element_by_tag_name('button')) which is your button
Besides that, the button in your example has two classes.
You click on a button via the .click() command.

Behat to click on element by class or ID name?

Is it possible for behat to find an element by class name to click on? It looks like only the following is searched for: id|name|title|alt|value
For example, how could you successfully identify this element to click on?
<a class="button medium round signup" href="http://link.com" data-reveal-id="signupModal">sometext</a>
Also here is a simple page with a button, that has an ID. How come the following does not access the button?
<button id="myBtn" type="button" onclick="myFunction()">Try it</button>
Given I am on "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_button_form"
When I press "myBtn"
Thanks
The button can not be accessed because it is contained in an iframe, you need to switch to that iframe to make the button available.
You can create steps to click on an element by any type of identifier (class included), or you can implement new step that requires css/xpath selectors.
Here you can find an example of a method to click by selector.
The first HTML code is nothing but a HREF. Behat should be easily able to identify it by using the line below:
Given I follow "sometext"
If the above doesn't work, you can simply use the CSS selector and click the element:
$locator = '.';
$this->getSession()->getPage()->find('css',$locator)->click();

selenium click using span class or onclick

I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.
<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>
I tried below codes but did not work and still getting the Unable to locate element error.
driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();
I have to click the homebutton. Any help would be much appreciated
I don't know why By.className("HomeButton") didn't work but you have errors in the other two.
In driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();
driver.findElement(By.xpath("//span[#onclick='javascript:onBtnHomeClick();']")).click();
If you want to use onBtnHomeClick() use contains
driver.findElement(By.xpath("//span[contains(#onclick, 'onBtnHomeClick')]")).click();
Or
driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();
And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
You simply need the correct locator IF your element will be eventually visible.
Xpath = "//span[contains(#class,'HomeButton') and contains(#onclick,'onBtnHomeClick')]"
Add wait as needed above exanmple, that should work.

How to automate to click a button which has no ID

<div class="add" data-icon="'" ng-show="!editMode" ng-click="btnAdd()"> Add an Honor or Award </div>
Above is the html code for the button i am trying to click (Add an Honor or Award), but this button doesn't have an unique ID, and keeps on changing the xpath as the user adds multiple data.
Use css
[class='add'][ng-click='btnAdd()']
xpath is also another option
//div[contains(.,'Add an Honor or Award')]
Or,
//div[#class='add']

unable to identify tooltip message present for the textboxes in selenium

i am trying to automate the login functionality of a site.I want to verify whether the tooltip is present or not and to capture the tooltip text displayed for the textboxes.The tooltip is displayed when trying to click on the login btn without filling the textboxes.The tooltip text is attached to the input elements via bootstrap javascript.No tilte attribute is present for the textboxes
https://elasticbox.com/login/ is the site address.Any ideas on how to capture the tooltip text .Thanks in advance
This is not a bootstrap tool-tip as you commented for #Varun's reply.
This is just the HTML5 form validation which comes into action when you put "required" as the attribute of textfield.
Make an html file 'test.html' file using the below code:
<html>
<body>
<form name='form1' post="http://www.google.com">
<input type='email' required placeholder='email address please' />
<input type='password' required placeholder='password please' />
<input type='submit' value='button1' />
</form>
</body>
</html>
Herein, when you click on "button1" (after opening the file in browsers like: Chrome, Firefox, etc.), you will see the necessary validation under the textfield(s).
But, there is no possible way to inspect them.
You can, however, use Sikuli/Autoit to check the presence of that validation text, but that again will be a lost cause as the image of the validation messages/tooltip differs from one browser to another.
Looks like developer needs to be consulted for this.
In javascript it seems like the signin button will remain disabled until both values are filled i.e. username and password.
I am not much into javascript, may be you can consult the developer in order to understand this more.
You can refer image below: