Xpath does not work with Selenium - java

I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.
The html looks like this:
...
<td class="tablerow" colspan="3">
<INPUT type=radio name="ticket" value="22" >ramdom1
<INPUT type=radio name="ticket" value="1" >ramdom2
<INPUT type=radio name="ticket" value="3" >ramdom3
<INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...
I was trying to select the input using xpath as follows:
String xpath = "//input[contains(#name, 'ticket') and contains(#value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));
But selenium doesn't found the element.
If change it to
String xpath = "//input[contains(#name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
or
String xpath = "//input[contains(#value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.
Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue.
I'm using Selenium WebDriver (2.37.1) with IE Driver.

Not sure whether this is a Selenium implementation issue but this should work:
"//input[contains(#name, 'ticket')][contains(#value, '3')]"
The use of and is basically the same so the result should be correct here.

I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault.
Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.
So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. Documentation on the front page of that website is very simple, and very easy to follow.
Once loaded in IE, open the Developer Tools and go into the Console. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.
Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:
document.evaluate("//input[contains(#name, 'ticket') and contains(#value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
The exact element you need will be returned, at least for me.
Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors:
input[name~='ticket'][value='3']

We can use the following css
1.css=input[value='22']
2.css=input[value='1']
3.css=input[value='3']
4.css=input[value='99']
For Checking the Radio Buttons.

Related

Selenium Xpath for Salesforce Page checkbox - “checked” attribute is not present

Issue: I am seeing difficulty to handle checkbox of Salesforce page. I am not seeing any difference
between two state of Checkbox. Both state checked and non-checked are showing same classes, attributes and text. I am not sure how to handle the checkbox on Salesforce.
Please see below screenshot. We do have Ids but they are dynamic so I can not use IDs in my test case.
Platform: JAVA, TestNG, Eclipse, Selenium
Only difference I see is when checkbox is selected then I can see below:
<span lightning-input_input="" class="slds-checkbox_faux" xpath="1">
: : after ==$0
</span>
when it's not checked the I can see below:
<span lightning-input_input="" class="slds-checkbox_faux" xpath="1">
</span>
What I tried so far which is not helping me on Salesforce page:
String rr = driver.findElement(xpath).getAttribute("checked"); // not working
isSelected(); // Not Working
document.getElementById('myInput').checked // Can not use this becasue of Dynamic ID
Below links I have gone through and tried locally on Salesforce page:
Selenium checkbox attribute "checked"
Xpath to determine checkbox "checked" attribute in Selenium IDE
Checkbox without checked attribute
#Mike ASP I had similar scenario on a salesforce platform. Try using the xpath as
//*[#name='Existing_Tenant__c'] It worked for me. If you see multiple elements use index to get the element you intend to check. For ex (//*[#name='Existing_Tenant__c'])[1]. Just make sure that the xpath is highlighting the type='checkbox' tag in the DOM.
The following image is my scenario

Can't find checkbox element by name contains, using Selenium Java

I am trying to click on a checkbox that only has a name.
Here is the HTML:
<td class="checkbox-column"><input type="checkbox" name="link-active[138]"></td>
Here is my latest attempt to find the element, I have tried a lot of different methods. I also need to grab it without the "[127]" part since that is dynamic in this case.
driver.findElement(By.xpath("//*[text()[contains(.,'link-active')]]")).click();
You are using wrong xpath, correct xpath is:
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
You need to check the name attribute.
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
xpath has a name attribute method, you can try this
driver.findElement(By.xpath("//*[contains(name(),'link-active')]")).click();
You can simply introduce webdriverwait to let your script knows that it is visible and enabled and then you can try to click. Like this :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#name,'link-active')]"))).click();

Selenide doesn't find the Web element

Could somebody help to understand what is wrong with element searching via Selenide.
I have such HTML code:
<div>
Current method:
<strong>
SMS
</strong>
</div>
The Selenium finds the element throw this xpath
findElement(By.xpath("//div[contains(. , \"Current method\")]/strong"))
but Selenide returns ElementNotFound exception with the same locator
$(By.xpath("//div[contains(. , \"Current method\")]/strong"))
If you need strong, you can just
$(By.xpath("//strong[contains(text(),'SMS')]"))
You can us following option.
$("strong:contains('SMS')"); Using css selectors method
You can try :
$(byText("Current method:")).shouldBe(Condition.exist);
Using Selenium->
findElement(By.xpath("//div[contains(text(), 'Current method')]/strong"));
Using Selenide->
$(By.xpath("//div[contains(text(), 'Current method')]/strong"));
These two method can be used in selenium as well as selenide.Methods are not specific to Selenium or selenide

Selenium sees textbox as hidden even if I can see it in the browser

I have a password textbox that is something like this
<input class="blahblah" id="someId" type="password"></input>
I am able to see this textbox in the browser and am able to manually insert password.
However when I test this ui using selenium, although it finds the element correctly, but when it tries to click the element, it throws an error
"org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with"
I did a check in code using
Boolean isDisplayed=el.isDisplayed();//false
Boolean isEnabled=el.isEnabled();//true
The isDisplayed came up false and isEnabled came up true. There is a 15 second delay added to give the page enough time to load (the page loads instantly). So adding a delay will not fix the problem.
I verified using firefox developer tools that the id it was finding was of the correct element.
Why does selenium think its invisible even if I am able to see it in the browser? Could it be that one of the parent elements has some style attribute that selenium doesn't like? Or is it a bug in the selenium driver?
I am using selenium driver for java version 2.45.0
The problem is that the desired input is really invisible due to the display: none being set on it's parent table:
<table title="Type a password."
class="dxeTextBoxSys dxeTextBox_MyCompany "
id="ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB"
style="width: 100%; border-collapse: collapse; display: none;"
border="0" cellpadding="0" cellspacing="0">
Most likely, the table is becoming visible on a particular user action that you need to determine.
But, alternatively, you can make the table visible through javascript:
WebElement table = driver.findElement(By.id("ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].style.display = 'block';", table);
In case the above doesn't make any difference.
There is an another hidden password input that can be important:
<input value=""
name="ctl00$ctl00$MasterContent$MainContentPlaceHolder$ViewCredentials$TopicPanel1$credentialGrid$editnew_4$txtPassword$P$PB$CVS"
type="hidden">
You can try making it visible and sending keys to it:
WebElement password = driver.findElement(By.name("ctl00$ctl00$MasterContent$MainContentPlaceHolder$ViewCredentials$TopicPanel1$credentialGrid$editnew_4$txtPassword$P$PB$CVS"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute('type', 'text');", password);
password.sendKeys("MyPassword");
In case the above doesn't work.
You can set the input value through javascript:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB_I').setAttribute('value', 'MyPassword');");
Possibly, Selenium is going too fast through your DOM. Has happened with me several times and your element hasn't fully loaded into DOM.
I am more familiar with the PHP/PHPUnit libraries available for Selenium, but perhaps you can introduce a temporary wait with a command similar to waitForElementPresent.
Also, if you have control of the code, can you give a 'name' attribute to your input field as well? It could not hurt anything to do so.
Have a look at the DOM elements and verify that there is no parent element with a display: none etc, when i encountered an issue like this that was the problem.
Are you able to get information from the element by XPath? This was my work around.
I have faced this kind of issue many times. the first thing comes into my mind is probably the selector you are using is not unique or not returning THE element you are looking for. Since,
Boolean isDisplayed=el.isDisplayed();//false
Boolean isEnabled=el.isEnabled();//true
does not return NoSuchElement exception I do not think it's a element load issue. A quick check can tell you what's going on
driver.findElements(By.cssSelector("the css")).size(); and see how many count it returns.

Check button is selected using selenium

I have HTML like this:
<input data-errorqtip="" aria-invalid="false" id="checkbox-1371-inputEl" class="x-form-field x-form-checkbox x-form-cb" autocomplete="off" hidefocus="true" type="button">
In UI it is showing like checkbox. How to check if it is selected?
I am using bellow code but is not working.
if (!field.isSelected()) {
field.click();
}
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
driver.findElement(By.id("idOfTheElement")).click();
}
Looks to me like you are using EXTjs. And while it renders as a checkbox, EXT does not set the input element value to selected so selenium won't know about it.
You will need to use selenium's ExecuteScript method to run javascript against EXT to determine if the item is selected or not.
I'd dig up the code, but I use EXT, Selenium, and C# and I have a feeling the syntax is going to be a bit different between the two, but the above is the general steps you will need to go through to get the result you are looking for.

Categories