Unable to find a radio button in selenium webdriver - selenium

I am unable to select radio button in Selenium Webdriver-Java with given html code
<input id="idcc-de81e53f-7cfd-4136-816f-d09d4055eeee" type="radio" value="de81e53f-7cfd-4136-816f-d09d4055eeee" name="panels:0:panel:stepContainer:stepTypeDisplay:optionPanel:options">
<label for="idcc-de81e53f-7cfd-4136-816f-d09d4055eeee">Canada</label>
<br>
<input id="idcc-17c1d432-5cec-4da9-9a02-39986d508770" type="radio" value="17c1d432-5cec-4da9-9a02-39986d508770" name="panels:0:panel:stepContainer:stepTypeDisplay:optionPanel:options">
<label for="idcc-17c1d432-5cec-4da9-9a02-39986d508770">United States</label>

id="idcc-de81e53f-7cfd-4136-816f-d09d4055eeee" most likely to by dynamic id so what you need to do is something like
option 1:
List<WebElement> radioButtons = driver.findElements(By.xpath("//input[#type='radio']"));
foreach(IWebElement button : radioButtons)
{
if(button.getText.Equels("Canada"))
{
button.cilck();
}
}
option 2:
driver.findElement(By.partialLinkText("Canada")).click();

Hi please do it like below
driver.get("file:///C:/Users/rajnish/Desktop/radio.html");
driver.manage().window().maximize();
// for canada
driver.findElement(By.id("idcc-de81e53f-7cfd-4136-816f-d09d4055eeee")).click();
// for United states
driver.findElement(By.id("idcc-17c1d432-5cec-4da9-9a02-39986d508770")).click();
what issues you were facing in doing this its very simple and straight ,please post your sample code that you have tried so that i can help you were you were doing it wrong thanks

Related

Xpath not working as expected while running the test

I am trying to automate the browser, while I try to locate the element via xpath in browser in static mode it is able to highlight the element where as when I run the script it comes back with an error that it is unable to find the element.
xpath I have written:
driver.findElement(By.xpath("//input[#value='soa_b_pbtv_l0_trnkni']/following-sibling::td[1]/child::select[#name='jobaction']")));
Here is the HTML:
<form name="f2" onsubmit="return verify();" action="/ATS/cgi-bin/barcap_jobaction.pl" method="post">
<>
<input name="jobname" type="hidden" value="soa_b_pbtv_l0_trnkni"/>
<input name="jobinstance" type="hidden" value="D03"/>
<input name="jobproceed" type="hidden" value="web"/>
<td style="background-color: #ffffff;">
<select name="jobaction">
if you're trying to select the select, jobaction then try this:
use css selector for the select select[name='jobaction']
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='f2']")));
List<WebElement> eleList = driver.findElements(By.cssSelector("form[name='f2']")));
for(WebElement element: eleList) {
if(element.findElement(By.cssSelector("input[name='jobname']")).getText().equalsIgnoringCase("expectedValue")) {
WebElement element = element.findElement(By.cssSelector("select[name='jobaction']"));
}
}
The INPUT is hidden so it won't be found using typical Selenium means. Selenium was designed to only interact with elements that a user can see and interact with. You are able to locate it in the browser because you are using JS or JQuery and they are not designed to ignore hidden elements. One way to get around this is to use JavascriptExecutor... it basically allows you to run JS in Selenium and find hidden elements. Since it sounds like you already have successful locators, I would suggest you look up some tutorials on JSE and you should be set.
If you run into a new issue while using JSE, come back and post a new question and we can try to help you.

How to deal with checkbox using selenium webdriver?

i want to scrape a page with checkboxes with no id, they have the same name and just the values are differents.
<div class="mvNavLk">
<form class="jsExpSCCategories" method="post" name="ExpressSCCategories" action="actionTest.html">
<ul class="mvSrcLk">
<li>
<label class="mvNavSel mvNavLvl1">
First
<input type="checkbox" value="firstValue" name="selectedNavigationCategoryPath">
</label>
</li>
<li>
<label class="mvNavSel mvNavLvl1">
Second
<input type="checkbox" value="secondValue" name="selectedNavigationCategoryPath">
</label>
</li>
</ul>
</form>
</div>
use below code:
driver.find_element_by_css_selector(".mvSrcLk>li:nth-child(1)>label.mvNavSel.mvNavLvl1").click();
hope this will work.
Hope this works-
driver.FindElement(By.XPath(".//label[contains(text(),'First')]/input")).SendKeys("test");
Hi please do it like below Note this example is in java
// take check boxes with same name inside the list
List<WebElement> myCheckBox = driver.findElements(By.name("selectedNavigationCategoryPath"));
// now on the basis of index call click
myCheckBox.get(0).click(); // for the first check box
Thread.sleep(2000);
myCheckBox.get(1).click(); // for the second check box
or if you want to select on the basis of value then
driver.findElement(By.xpath("//*[#value='firstValue']")).click(); // for the 1st one
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#value='secondValue']")).click(); // for the 2st one
UPDATE
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#value='firstValue']")));
driver.findElement(By.xpath("//*[#value='firstValue']")).click(); // for the 1st one
Hope this helps you
driver.findElement(By.name("selectedNavigationCategoryPath")).click();
Below Code is in C#, Collects all the Checkboxes with the name specified. Then Iterates through each ckeckbox, gets the value attribute, if the attribute is equal to your specified check box, then clicks it and comes out of the loop. Hope this should work.
IList<IWebElement> myCheckBoxs = driver.FindElements(By.name("selectedNavigationCategoryPath"));
Foreach(IWebElement chkBx in myCheckBoxs)
{
if(chkBx.GetAttribute("Value")=="Your Desired value")
{
chkBx.Click();
break;
}
}
`
Use this CSS locator.
[name='selectedNavigationCategoryPath'][value='firstValue']

How to validate Web element if xpath locator is dynamic in selenium webdriver

ID and Xpath is changing for "OK" button every time while saving(Account).
HTML Code:
<div class="modal-footer" style="display: block;">
<div class="bootstrap-dialog-footer">
<div class="bootstrap-dialog-footer-buttons">
<button id="fe02d6bd-6058-4871-b0e1-c1e914f64a6a" class="btn btn- default">Ok</button>
</div>
</div>
</div>
</div>
Xpath:.//*[#id='fe02d6bd-6058-4871-b0e1-c1e914f64a6a']
"ID"/XPath is not constant and it is varying while saving.
use the below code:
driver.findElement(By.cssSelector("div.bootstrap-dialog-footer-buttons>button.btn.btn-default"));
You can devise your own XPath locator to find the OK button by it's text content like so:
//button[.='Ok']
The first part of the XPath expression - //button - will select all <button> WebElements within the currently focused content.
The second part - [.='Ok'] - is a predicate that will filter out any WebElements whose exact text content is not equal to 'Ok'.
If it is the only OK button available on the page then you can probably use below code.
driver.findElement(By.xpath("//button[contains(.,'Ok')]"));
Else you can take a reference of parent window and locate a button on it as below
WebElement modalWin = driver.findElement(By.id("modal-window-id"));
modalWin.findElement(By.xpath("//button[contains(.,'Ok')]"));
This below code helps to click OK button in any page.
just call this method with parameter saying OK
public void buttonClick(String buttonname){
WebElemennt button = driver.findelement(by.xpath("//button[text(),'Ok']"))
or
WebElemennt button = driver.findelement(by.cssselector(".btn btn- default"))
for(int i=0; i<button.size;i++)
{
if(button.get(i).gettext().equalIgnorecase(buttonname))
{
button.get(i).click
}
}
}
Let me know result..

Selenium send keys not working on upload button

I am trying to upload a file using selenium send keys, but not working, .In my case button name is Attach Sign Off , its not working for it. Please help
<form class="v-upload v-widget v-upload-immediate" enctype="multipart/form-data" method="post" action="https://gbl04115.systems.uk.hsbc:8571/DSLWeb/APP/UPLOAD/2/921/action/3305f203-9e0c-4213-aecd-6ee2b2b29eb1" target="921_TGT_FRAME">
<div aria-describedby="gwt-uid-2">
<input type="hidden"/>
<input class="gwt-FileUpload" type="file" name="921_file" aria-describedby="gwt-uid-2"/>
<div class="v-button" tabindex="0" role="button" aria-hidden="false" aria-describedby="gwt-uid-2">
<span class="v-button-wrap">
<span class="v-button-caption">Attach Sign-off</span>
</span>
Try this one and let me know if it not works
WebElement fileInput = driver.findElement(By.className("gwt-FileUpload"));
fileInput.sendKeys("C:/path/to/file.jpg");
I hope this answer will help to solve your issue. But i have not tested yet.
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
(OR)
driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg");
#AutoMater - You can update your code as below:
It should work as it is working for me. And give your test method priority as per your requirement. Just for Example I gave priority here as #Test(priority = 1). I hope it should work for you.
#Test(priority = 1)
public void CERTIFICATIONSSCREENUploadCertficationFilesValidation()
throws InterruptedException, AWTException {
//Click on File Upload Button
driver.findElement(By.xpath("//*[#id='certificationFile']")).click();
Thread.sleep(1000);
// Set the file name in the clipboard. Also following line of code will search file in your computer so make sure you provide correct file path.
StringSelection s = new StringSelection("C:\\Doc\\CertificationFile.xls");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s, null);
Thread.sleep(1000);
Robot robot1 = new Robot();
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
robot1.keyPress(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);
}
List elements = driver.findElements(By.className("gwt-FileUpload"));
elements.get(1).sendKeys(filePath);
It worked using above code.
Thanks #Andersson

How to check if radio button is selected or not using Selenium WebDriver?

<div class="my_account_module_content">
<h3 class="my_account_module_content_title">
Mi Dirección 1
<br>
<span>Predeterminada</span>
<div class="selectCard_left">
<input id="17390233" class="default_shipping_address" type="radio" name="address" checked="true">
<span>Seleccionar como tarjeta predeterminada</span>
</div>
this is the HTML code
If radio button selected is true then print the class span value?
please help me..
In Java, this would do it:
if(driver.findElement(By.id("17390233")).isSelected()){
System.out.println(driver.findElement(By.xpath("//input[#id='17390233']/following-sibling::span[1]")).getText());
}
If the radio button is selected, then the text will show. If you want to use the text somewhere, I suggest you put it in a string instead:
String spanText = driver.findElement(By.xpath("//input[#id='17390233']/following-sibling::span[1]")).getText();
Hope this answers your question.
EDIT: Here is an update of other ways to try.
If the className default_shipping_address is unique (e.g. not used anywhere else on the page), you may try locating the element by className:
if(driver.findElement(By.className("default_shipping_address")).isSelected()){
System.out.println(driver.findElement(By.xpath("//input[#class='default_shipping_address']/following-sibling::span[1]")).getText());
}
If that class is not unique, maybe the DIV's className selectCard_left is?
if(driver.findElement(By.className("selectCard_left"))){
System.out.println(driver.findElement(By.xpath("//div[#class='selectCard_left']/span[1]")).getText());
}
If none of the classNames are unique, a complete xpath expression is required. If you still are unable to get that text, I refer to reading up on how to use xpath: http://www.w3schools.com/XPath/xpath_syntax.asp
I hope that you find this information useful.