Xpath Changes regularly - selenium

I have an issue. When I select an xpath and run the test sometimes the first few times it works. After some time it fails, and when i go check the xpath again i discover that somethings in it has changed. This isn't a web application that is being updated constantly . What do you think the problem could be?
for example the name in the code changes regularly . Here it is turnOverAvailableInd1 later it may become turnOverAvailableInd2.
<td>Is turnover figure available?</td>
<td valign="baseline">
<input type="radio" name="turnOverAvailableInd1" value="Y" onclick="javascript:turnOverAvailableToggle(this);" id="turnOverAvailableInd1Yes">YES
<input type="radio" name="turnOverAvailableInd1" value="N" onclick="javascript:turnOverAvailableToggle(this);" id="turnOverAvailableInd1No">NO
</td>
This is how i select the radio button
Select Radio Button turnOverAvailableInd1 N

You can use the other locator like, ID, CSS Selector or different XPATH like below
//input[#id='turnOverAvailableInd1Yes'] // For YES radio button
//input[#id ='turnOverAvailableInd1No'] //For NO radio button
OR
You can use the Value like below
//input[#value='Y'] //For YES radio button
//input [#value= 'N'] //For NO radio button
OR
//*[contains(text()='YES')] //For YES radio button
//*[contains(text()='NO')] //For NO radio button
You can find more here

If I understand your question correctly, you want an xpath that can work whether then name is "turnOverAvailableInd" appended by some number. If so, the following xpaths could work:
//input[#value='Y' and contains(#name,'turnOverAvailableInd')]
//input[#value='N' and contains(#name,'turnOverAvailableInd')]

Related

ionrangeslider: disable drag the slider. Only want to use for display

I am using ionrangeslider to display some temperature value. I like the way it displays.
I dont want the user to slide it. How can i disable the slider
I didnt find any option to diable dragging
this is the html and the js i am using
<input type="text" class="js-range-slider" name="my_range" value=""
data-min="40"
data-max="210"
data-from="115"
data-grid="true",
data-hide-min-max="true",
data-postfix="'F"
/>
$('.js-range-slider').ionRangeSlider()
It shows as
if you want to fix the values top/from, you have option:
to_fixed:true,//block the top
from_fixed:true//block the from

selenium code for accessing button type submit in c#

I am trying to extract the text in an input box,
<input value="Add a Phone" onclick="CSS.addClass($("u_0_4"), "async_saving"); new AsyncRequest().setURI("\/ajax\/phone\/confirmation").setData({source: "www_mobile_settings"}).setStatusElement($("u_0_4")).send();" type="submit" id="u_0_5">
I have tried for ;
driver.FindElement(By.Id("u_0_5")).Click()
but raising an error of : No such element Unable to locate Element
The element Add a Phone is present inside an iframe.You need to switch the iframe first in order to access the element.
driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("iframe[src^='https://www.facebook.com/settings?']")));
driver.FindElement(By.CssSelector("input[value='Add a Phone']")).Click();
In order to get that button, you can use CSS selector here is one
driver.FindElement(By.CssSelector("input[type='submit'][value='Add a Phone']")).Click()
or by XPATH
driver.FindElement(By.XPath("//input[contains(#value,'Add a Phone')]")).Click()

Selecting from 2 Radio buttons when 1 button has no id - Webdriver

I am trying to write a script in web driver and part of the page that I am testing has 2 radio buttons where the person has to agree that something is correct.
By default the no button is selected and the user has to select the yes radio button to continue.
I have read a number of solutions for things similar and I've tried numerous different things but still can't get Selenium to select the yes radio button. It is a Yes/No question. The No field is already pre-populated but I need it to select Yes.
As you can see below there is a small amount of code for this part of the form.
<pre>
<form action="">
<input id="selfCert" name="selfCert" value="no" checked="checked" type="radio">No
<input name="selfCert" value="yes" type="radio">Yes
</form>
</pre>
I have tried to use the xpath and the css but the test just terminates.
Any advice would be greatly appreciated.
Thanks
Gareth
Can u use xpath to select the Yes radio button?
As both input fields share the same name="selfCert" I think u need to use xpath then.
I have an example in C# for Selenium, if you're using a different language you will need to modify it.
YesRadioButton = webDriver.FindElement(By.XPath("//form/input[2]"));
YesRadioButton.Click();
I have managed to resolve the issue now using a wait command until webdriver can see the button
WebElement element = driver1.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/div[5]/div[1]/div/form/input[2]"));
WebDriverWait wait = new WebDriverWait(driver1, 20);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();

Click on a specific part of HTML element

I have a checkbox element on my web-page which has a partial link which reads as Read the conduct risk manual where in conduct risk manual is a link which when clicked on navigates to page that has conduct risk manual listed down.
The HTML structure for this element is as below:
<div class="checkbox">
<label>
<input ng-disabled="readonly" type="checkbox" name="policyRead" required="" ng-model="confirmOa.confirmation.policyRead" class="ng-pristine ng-untouched ng-invalid ng-invalid-required xh-highlight">
<span>Read the <a target="_blank" href="http://somesite.xyz/Conduct%Risk%Manual.pdf">Conduct Risk Manual</a></span>
</label>
</div>
Now I want to click on a check-box which used to work if I click on span element but now even if I try with below combinations of web-driver is still clicking on link Conduct Risk Manual
1) .//div/label/input[#name='policyRead']
2) .//input[#name='policyRead' and #type='checkbox']
3) .//span[contains(text(),'Read the')]
4) .//span[contains(text(),'Read')]
5) .//input[#name='startDateConfirmed']//preceding::span[1]
In the 5th xpath I have tried clicking on span element using the following HTML element which is correctly identified and clicked.
I have tried many combinations apart from those mentioned above and all the xpath's correctly highlight the expected element which means it is correctly identified. But still web-driver clicks on Conduct Risk Manual link and navigates to a new page instead of clicking corresponding checkbox.
Can we make web-driver click on a specific part of an element?
Please check xpath's wherein I have already tried using only 'Read' or 'Read the' portion of span element.
Try this below code.
Explanation: Use text method with span tag, then move ahead with preceding-sibling keyword to find input tag for clicking the checkbox element.
Note: span tag use here for reference to get the check-box element text.
WebElement checkbox = driver.findElement(By.xpath("//span[contains(text(),'Read the')]//preceding-sibling::input[#name='policyRead']"));
if(!checkbox.isSelected()) // if checkbox is not selected, then only if is condition will execute.
{
checkbox.click();
}
OR
Using Java-script executor to click the checkbox web-element.
WebElement checkbox = driver.findElement(By.xpath("//span[contains(text(),'Read the')]//preceding-sibling::input[#name='policyRead']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", checkbox);
You can try with this code also as a tag is coming over to the checkbox.
WebElement checkBox= driver.findElement(By.xpath("//span[contains(text(),'Read the')]//preceding-sibling::input[#type='checkbox']"));
Actions action = new Actions(driver);
action.moveToElement(checkBox).click().build().perform();`
Also add import org.openqa.selenium.interactions.Actions; .

Checkbox id keeps changing in webpage causing selenium script to fail

I am trying to automate a click on a certain checkbox. However after every run the checkbox id changes and script fails to find the element. Is there an alternate way of writing an xpath
<span id="field_key$0993573c-83b4-30d4-9139-44e44b496d0f$1food_contamination-checkbox" class="v-checkbox v-widget" ca-help-field-id="undefined">
<input id="gwt-uid-193" type="checkbox" value="on" tabindex="0" checked=""/>
<label for="gwt-uid-193"/>
</span>
xpath I used was this:
//*[#id='gwt-uid-193']
If the parent <span> id is fixed you can use it and go one level down to the checkbox
driver.findElement(By.cssSelector("#field_key$0993573c-83b4-30d4-9139-44e44b496d0f$1food_contamination-checkbox > input"));
Or use the parent class
driver.findElement(By.cssSelector(".checkbox > input"));
And if you have only one checkbox you can use the type attribute
driver.findElement(By.cssSelector("[type='checkbox']"));
I found a solution myself. We can use xpath based on the position of the checkbox, i.e.:
// input[#type='checkbox'])[position()=3]
This can be used for radio buttons as well. Replace checkbox with radio.