Selenium *ElementPresent and *XpathCount give different results? - selenium

I am getting different results for the same locator. For example
//table[#id='foo']
returns true when testing ElementPresent, but returns 0 for XpathCount. In Selenium v1.0.10 IDE the Find button highlights the correct element for both functions. Any ideas on what could be causing this?
Notes:
We have frames on the page EDIT: This is probably the problem. Bounty to verification.
There are many tables on the page, but only one with #id of "foo"
Firefox 3.6
Happens in both IDE and Java RC

Well, this is not a verification more of a non-verification.
I use Selenium to test a GUI with frames. To make isElementPresent and getXpathCount to work I always have to select a frame first with selectFrame (even to get isElementPresent to work correctly). By just opening an URL no frame at all seems to be selected.
This is what the HTML and corresponding selectFrame code looks like:
<frameset id="mainframeset"><frame name="nav" id="nav" src....
selenium.selectFrame("nav");

Use these XPath expressions:
boolean(//table[#id='foo'])
and
count(//table[#id='foo'])
In case there is a table element whose id attribute's value is "foo", then the first expression above should evalute to true() and the second expression above should evalute to a positive integer.

Not really a direct answer to the question, but a workaround if you are reading this and want to loop over the elements. Use isElementPresent in the for loop like this:
for(int i = 2; selenium.isElementPresent("//table[#id='foo']//tr["+i+"]"); i++)
{
selenium.getText("//table[#id='foo']//tr["+i+"]//td["+columnNum+"]");
}
Note that we start i at 2 since XPath is indexed from 1 and we want to skip the header

Related

Ignore case Xpath #Name attribute c# Selenium/Appium

I have a C# selenium/Appium project where I need to find a desktop Application window By.Xpath("").
This works:
By.XPath("//*[#Name='ASDASD']");
However, some builds of the app have the window name be "ASDasd", which causes the Xpath above to not find the window element and the test fails.
Is it possible to Ignore the case of the #Name attribute whether it be "ASDASD", "ASDasd" or something else?
I did try using the XPath translate function, but I am not able to find the element, I assume I am doing it wrong.
What I tried:
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
or
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd']")
or
By.XPath("//*[#Name='translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd'']")
or
By.XPath("//*[#Name='translate(asdasd,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')']")
Maybe some other variations too, but I could not get it to work.
Some of the examples may have invalid formatting.
While other seems to be valid but could not find the element and it would timeout.
UPDATE:
Thank you for the assistance, this worked:
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='asdasd']");
However, it added 60 seconds to the test somehow, it seems to stall for 60 seconds on one of the places where it looks for the main window.
Thanks for the help!
Regards
name() gives you the name of context node. In this case (//*), the name of whatever element you are currently looking at. You meant to write #Name, i.e. the attribute that happens to be called Name.
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
Using translate() is clunky and fails when the search string contains unanticipated characters.
Unfortunately, there is no lower-case() function in XPath 1.0. But you can work around this limitation with the help of the host language (such as C#).
The following will dynamically create an XPath expression which finds arbitrary values case-insensitively:
var searchValue = "asdasd";
var uc = searchValue.ToUpperInvariant();
var lc = searchValue.ToLowerInvariant();
var xpath = $"//*[translate(#Name, '{uc}', '{lc}') = '{lc}']";
// -> "//*[translate(#Name, 'ASDASD', 'asdasd') = 'asdasd']"

selenium webdriver_Is there any other way to find out the count of webelements present in a webpage without using "findElements()" method?

Is there any other way to find out the count of webelements present in a webpage without using "findElements()" method?
This Question is asked in Interview. So I would like to know whether it is possible to get the count of Webelements without using findElements().
Update to #mate and #custom answer,
using xpath, to extract the number.
driver.executeScript(() => $x('count(//div)'));
if there is 25 div elements, will return 25 as a number.
Here's one way:
Go to DevTools
Enter $x('//*') to console
You will get the number of HTML elements
You could use WebDriver.prototype.executeScript which lets you execute some JavaScript code. That code can either be a function or a string:
driver.executeScript(() => document.querySelectorAll('*').length);
PS: I'm using the JS flavour of selenium-webdriver.

Selenium is not identifying the 'iframe'

Below in my code
driver.switchTo().frame(driver.findElement(By.id("file")));
Above line is not running
There is various way to switch to frame. Please share the HTML code if you need a exact code to switch to your respective application. you can try with below method. Better use index if your frame do not any name etc
driver.switchTo().frame(index)
replace index with 0 first and if it not work then try with 1 and then 2 etc one by one
More details
driver.switchTo().frame() has multiple overloads.
driver.switchTo().frame(name or id)
Here your iframe doesn't have id or name, so not for you.
driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try
driver.switchTo().frame(0)
driver.switchTo().frame(iframe_element)
The most common one. You locate your iframe like other elements, then pass it into the method.
Here locating it by title attributes seems to be the best.
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Fill Quote']")));
// driver.switchTo().frame(driver.findElement(By.xpath(".//iframe[#title='Fill Quote']")));
most probably, your iframe is not visible or your window is not active.
in Python:
driver.switch_to_default_content()
wait.until(EC.frame_to_be_available_and_switch_to_it("yourFrame"))
in Java:
driver.switchTo().defaultContent();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("yourFrame")));

How can i select element in selenium webdriver by part of ID.

I have a td element with ID like "a:2:3:d:", and when i want to select it by id, my webdriver can not find it. Is there a way to search by part of ID, because I think, that the problem is at last ":" in the identifier.
First, you need to confirm that this really is the problem, and it's not just that the page isn't fully loaded, or is loaded asynchronously. I don't see any particular reason why Selenium should care about the trailing ":".
Update: From the comments, it's much more likely that the dynamic id that is the problem, but the solution is the same either way:
To find an element by partial id, you can use xpath. If you were trying to find a div by partial id, for example:
//div[contains(#id, 'a:2:3')]
You don't say what language you are using, but in python, this would be used as follows:
driver.find_element_by_xpath("//div[contains(#id, 'a:2:3')]")
and in Java:
driver.findElement(By.xpath("//div[contains(#id, 'a:2:3')]"))
Assuming you are using Java
WebElement el = driver.findElement(By.cssSelector("td[id*='a:2:3']"));
The above code gets the element of TD which starts with a:2:3 as we use * in the css Selector.
XPath is more powerful and can be sometimes difficult to understand. CSS Selector is easy.

Finding text on page with Selenium 2

How can I check whether a given text string is present on the current page using Selenium?
The code is this:
def elem = driver.findElement(By.xpath("//*[contains(.,'search_text')]"));
if (elem == null) println("The text is not found on the page!");
If your searching the whole page for some text , then providing an xpath or selector to find an element is not necessary. The following code might help..
Assert.assertEquals(driver.getPageSource().contains("text_to_search"), true);
For some reason, certain elements don't seem to respond to the "generic" search listed in the other answer. At least not in Selenium2library under Robot Framework which is where I needed this incantation to find the particular element:
xpath=//script[contains(#src, 'super-sekret-url.example.com')]
A simpler (but probably less efficient) alternative to XPaths is to just get all the visible text in the page body like so:
def pageText = browser.findElement(By.tagName("body")).getText();
Then if you're using JUnit or something, you can use an assertion to check that the string you are searching for is contained in it.
assertThat("Text not found on page", pageText, containsString(searchText));
Using an XPath is perhaps more efficient, but this way is simpler to understand for those unfamiliar with it. Also, an AssertionError generated by assertThat will include the text that does exist on the page, which may be desirable for debugging as anybody looking at the logs can clearly see what text is on the page if what we are looking for isn't.