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
Related
How to get exract xpath for the following name(I1888 - Child 1.1). now i am using following xpath "//span[contains(#class,'TreeTitleRed')][contains(.,'Child 1.1')]" but i need to get xpath without contains. please help me
<div>
<span> class ="Vegan tree"
<span class="treeTitlered">I1888 -Child 1.1</span>
</span>
</div>
You can use the following if you want it in xpath
//span[#class='treeTitlered']
Or you can directly find the locator by using the classname
driver.findElement(By.className(“treeTitlered”));
Hope this helps.
You can also use.
//div/child::span[1]/child::span[1]
This will surely work for you, do let me know in case of any further queries.
You can also use
//div/span/child::span[#class="treeTitlered"]
You can stick to parent <span> tag and locate it using its class attribute of Vegan tree.
Once done you can query its innerText property which will return I1888 - Child 1.1 text
Example code (assumes WebDriverWait just in case)
WebElement veganTree = new WebDriverWait(driver, 10)
.until(ExpectedConditions
.presenceOfElementLocated(By
.xpath("//span[#class='Vegan tree']")));
System.out.println(veganTree.getAttribute("innerText"));
Demo:
i'm tried to select an element from an auto suggestion field but i got always an error saying that the element could not be found even that i'm sure my xpath is correct
here's my code :
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=\"ui-menu-item-with-icon ui-menu-item\"][1]")));
driver.findElement(By.xpath("//*[#class=\"ui-menu-item-with-icon ui-menu-item\"][1]")).click();
it should find //*#class=\"ui-menu-item-with-icon ui-menu-item\" which is the first suggestion albert cammus
here's the outerHtml
<li class="ui-menu-item-with-icon ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">
<span class="item-icon"></span>
Albert Camus (SARCELLES)</a>
</li>"
Your XPath is more or less OK apart from using wildcard which may result into longer processing so you can go for li instead of *.
Another option is sticking to the <a> tag containing the text you would like to click using normalize-space() function something like:
//a[normalize-space()="Albert Camus (SARCELLES)"]
Also your popup may reside within an iframe so you might have to switch the webdriver context to the relevant iframe element.
Why don't you try linkText over Xpath ?
linkText is more stable then Xpath, there's no doubt about that.
Code :
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Albert Camus (SARCELLES)")));
I'm not very sure about spaces in your HTML, that's the reason why I have used partialLinkText
The problem that I am facing is that I can't seem to select that div class using XPath.
<div class="_3WZoe"><span dir="auto" class="">We can not found, try again.</span></div>
How can i do XPath, i don't know.
Thank You.
//div[contains(#class,"_3WZoe")]
When recorded in Selenium IDE, the result is this:
click
//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/hd/font/span/acronym
Here is my entry in Eclipse using WebDriver(Java)
driver.findElement(By.xpath("//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/h2/font/span/acronym")).click();
Here is the error I receive:
no such element: Unable to locate element:
{"method":"xpath","selector":"//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/h2/font/span/acronym"},
HTML
<span onclick="change_site"('SITES');" style="cursor:pointer; color:#12345; text-decoration:underline;">
<acronym title=Site ID:">DEFAULTSITE</acronym>
</span>
As per the HTML provided you can directly access the acronym element
driver.findElement(By.xpath("//span/acronym[#title='Site Id:']")).click();
Try with below xPath :
driver.findElement(By.xpath("//acronym[contains(text(),'DEFAULTSITE')]").click();
Try any of the below xpath
driver.findElement(By.xpath("//acronym[text()='DEFAULTSITE']");
driver.findElement(By.xpath("//span/acronym[text()='DEFAULTSITE']");
Or this css
driver.findElement(By.css("acronym[title='Site']");
If none of the above Xpath has worked for you ,please try the following Xpath
//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/hd/font/span/*[name()=acronym]
Please let me know if this has helped you or not.
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.