How to use AND condition in Robot Framework? - selenium

I have the HTML content as below:
<input type="radio" name="radioButton" value="on" id="radio1" tabindex="0">
<input type="radio" name="radioButton" value="on" id="radio2" tabindex="0">
<input type="radio" name="radioButton" value="on" id="radio3" tabindex="0">
Reference:
How to use OR condition for Keywords in Robot Framework?
I am currently trying the logic mentioned in above link.But it's not most relevant to my scenario.
I would like to fetch and select the radio button using it's type, name and id or class.
I want to achieve something like below:
Select Radio Button if //input[#type="radio"] AND //input[name="radioButton"] AND //input[#id="radio2"]

You don't need an if statement. Simply put all of your conditions in a single xpath. For example:
select radio button //input[#type='radio' and #name='radioButton' and #id='radio2']
Though, if the id is unique then you don't need all that. If id is unique you can use something like:
select radio button //input[#id='radio2']

You can do the next
WebDriver driver = new FirefoxDriver(); //it can be any driver, not specifically firefox
WebElement radioButton = driver.findElement(By.xpath("//input[#type='radio' and #name='radioButton' and #id='radio2']");
radioButton.click();
The above function will look for the element and click it. And as #Bryan says, if the id is unique you can look for the element only by its id.
WebDriver driver = new FirefoxDriver(); //it can be any driver, not specifically firefox
WebElement radioButton = driver.findElement(By.id("#id='radio2']");
radioButton.click();

Related

How to select a option from the list which is displayed inside 'input' tag instead of 'Select' tag through Selenium WebDriver

Using Selenium Webdriver and Java, I want to select a option from the list which is displayed inside 'input' tag instead of 'Select' tag.
Please find below details-
The GUI is as following-
Please find the GUI image here
<td nowrap="" valign="middle" colspan="3" rowspan="1">
<div class="mceGridField siebui-value mceField">
<input type="text" name="s_6_2_158_0" value="" aria-labelledby="BGC_Type_Label" aria-label="Type" style="height: 24px; width:80px;" class="siebui-ctrl-select siebui-input-popup siebui-align-left siebui-input-align-left ui-autocomplete-input" aria-describedby=" s_6_2_158_0_icon" maxlength="30" tabindex="0" role="combobox" autocomplete="off" data-seq="0" aria-readonly="false">
<span class="siebui-icon-dropdown applet-form-combo applet-list-combo" id="s_6_2_158_0_icon" data-allowdblclick="true"></span></div>
</td>
The GUI List of options are as following-
Please find the GUI image of option list after clicking
I am using below code to select one of the option from the list-
driver.findElement(By.xpath("//*[#id='s_6_2_158_0_icon']")).click();
But still I am not able to select any option.
Can you please help ?
To invoke click() on the desired option from the list you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name^='s_'][aria-labelledby='BGC_Type_Label'][aria-label='Type']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#name,'s_')][#aria-labelledby='BGC_Type_Label' and #aria-label='Type']"))).click();
Did you try sendKeys method btw-
driver.findElement(By.xpath("//*[#id='s_6_2_158_0_icon']")).sendKeys("Fix");
And it's better to use the id -
driver.findElement(By.name("s_6_2_158_0_icon")).sendKeys("Fix");

how to get element inside custom tag in chrome web driver

This is my sample html code.
<div class="content">
<M class="mclass">
<section id="sideA">
<div id="mainContent">
<div class="requestClass">
<span>Check</span>
<input type="text" id="box">
</div>
</div>
<section>
<section id="sideB">
...
<section>
</M>
</div>
I want to set some value to my text field ("box"). So I tired to set like below code
driver.findElement(By.xpath("...")).sendKeys("SetValue");
My Xpath id is correct, it's exist in the page but am getting this error
no such element: Unable to locate element: {"method":"xpath","selector":"id("..."}
Why I am getting this error because of my custom tag,if yes how to get element inside custom tag?
As per the HTML you have provided to fill in some value to the text field represented by <input type="text" id="box"> you can use either of the following line of code:
cssSelector :
driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue");
xpath :
driver.findElement(By.xpath("//section[#id='sideA']//input[#id='box']")).sendKeys("SetValue");
If you still want to use XPath. This worked for me-
driver.FindElement(By.XPath(#"//*[#id='box']")).SendKeys("AB‌​");
I don't think the custom tag causes any problem as the CssSelector also works-
driver.FindElement(By.CssSelector(#"m[class='mclass'] input")).SendKeys("AB");
You can use ID or xpath to locate it, My suggestion you have to use ID. Also use Explicit wait till element to be visible.
Using ID, your code is like this:
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.sendKeys("test");
You can also use JavascriptExecutor
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='test';", elem);

Selenium radiobutton selection

I have an HTML code as follows:
<label class="top" for="id_gender2">
<div id="uniform-id_gender2" class="radio">
<span>
<input id="id_gender2" type="radio" value="2" name="id_gender">
</span>
</div>
Mrs.
</label>
The radio button is getting selected after mouse hover.
I have tried with all possible attributes for selection but I am getting element not found exception.Please let me know how to write java script in webdriver.
Try following code and let me know the result:
Actions action = new Actions(driver);
WebElement hover = driver.findElement(By.xpath("//*[#class='radio hover']"));
action.moveToElement(hover).moveToElement(driver.findElement(By.xpath("//input[#id='id_gender2']"))).click().build().perform();
UPDATE
Actually there is no need in mouse hover. Target radio seem to be initially non-clickable, so you just need extra time to wait for page complete rendering:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#id_gender2")));
driver.findElement(By.cssSelector("input#id_gender2")).click();
Try javascript executor as follow:
WebElement element = driver.findElement(By.id("id_gende‌​r2"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
Your element is probably not loaded properly while getting error. You'd better use explicit wait in your script. Wait until your specific element loaded to be visible/clickable. I think, this may help you.
Code snippet:
By yourElementToSelect = By.id("id_gender2");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(yourElementToSelect));
driver.findElement(yourElementToSelect).click();

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 can I locate this element using Xpath?

How to locate the element below in Selenium?
<input id="mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R" class="iceSelInpTxtTxt fieldTxt" type="text" value="" style="width: 150px;" onmousedown="this.focus();" onfocus="setFocus(this.id);svOnFocus(formOf(this), this, event, false);" onblur="setFocus('');svOnBlur(formOf(this), this, event);" name="mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R" autocomplete="off">
I tried with ends-with, it did not work.
You can use the below
WebElement inputClass = driver.findElement(By.className("iceSelInpTxtTxt fieldTxt"));
WebElement Element = driver.findElement(By.xpath("Try any Below xpaths"))
If your input id is unique then use below xpath
//input[#id='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R']
OR
//input[#name='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R']
OR if the combination of both of id and name make them unique then use below xpath
//input[#name='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R' and #id='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R']
OR
//input[#class='iceSelInpTxtTxt fieldTxt']
Hope it will help you :)