Xpath

Solving Unable to locate element error in HTML5 

I am currently working on a HTML5 project and I encountered an error “Unable to locate element:”. I tried using xpath, className and even cssSelector. I was getting the same error.
I then used advanced webdriver org.openqa.selenium.interactions; Class Actions and that solved the problem.
Here is the code:
new Actions(driver).moveToElement(
driver.findElement(By.className("m-textarear"))).sendKeys("Selenium Wiki").perform();

By Pavandeep Puddupakkam on October 25, 2012 | Java, WebDriver | A comment?
Tags: , , , , , ,

How to locate an element by Xpath with its dynamic title 

Example: I have two images
ImageHTMLCode
some_date1 and some_date2 are generated dynamically.
How to locate them using CSS locator ?
click css=img[title*='since'] : will not work because it won’t
make difference between the two images.

Here is the solution:
If you want to click on the first try this : //IMG[contains(@title,'Critical since')][1]

By Pavandeep Puddupakkam on March 23, 2012 | Selenium RC, XPath | 3 comments
Tags: , , ,

Using Xpath when you have new line text 

Here is an example where there are new lines and have to validate if the exact text is displayed in the correct order using xpath.

User-agent: *
Disallow: /clickthrough.html
Disallow: /details.html
Disallow: /post-sendtofriend.html

Use normalize-space(), which will throw away the leading and trailing whitespace characters (and condense repeating spaces in the middle of the text into a single space), so that you can compare the normalized text() and use to filter in a predicate.
Here is the xpath code to validate the above text

try
{
selenium.getText("//test[normalize-space()]").Equals("pass the string here");
}
catch(Exception)
{
}

By Pavandeep Puddupakkam on July 13, 2011 | XPath | A comment?
Tags: , ,

Using regular expression in Selenium 

Here is the xpath I want to use regular expression.
Code with out using regular expression:
Assert.IsTrue(selenium.IsElementPresent(“ctl00_5hh5_g5656_675gffg042f_tbSearch”));
Code using regular expression:
Assert.assertTrue(selenium.isElementPresent(“css=input[id$='tbSearch']“));

Alternative is to use contains in the xpath. Here is the example:
//input[contains(@id,'tbSearch')]

By Pavandeep Puddupakkam on July 4, 2011 | XPath | A comment?
Tags: ,

Using regular expressions in xpath Selenium 

Here is an example where I have used regular expressions in xpath.
This article includes a code sample that demonstrates how you can use the starts-with XML Path Language (XPath) string function to implement this requirement.

Here is my HTML source code:

Abrasion Resistance

Access Panels

Access Solutions

Accessories

Acoustic Performance

Affordable Playground Equipment

Aged Care Facilities

Aggregates

Air Circulation

Air Conditioning Filter Cleaning

Air Conditioning Maintenance

Air Conditioning Services

Air Flow

Air Fresheners

Air Movement

Alarm Monitoring

Alfresco

Alternative Waterproofing

Aluminium Composite

Amplifiers

Anodising

Anti Slip

Anti Vandal

Anti-graffiti

Anti-Rust

Apartments

Appliances


Here is the verifyElement command in Java to verify for the xpath element.
verifyTrue(selenium.isElementPresent(“//*[starts-with(@id, \"c100Item_P\")]“));

By Pavandeep Puddupakkam on October 14, 2010 | Selenium RC | 2 comments
Tags: ,

How to use Verifyelementpresent in Selenium 

You can use selenium.IsElementPresent(“element”) to see if a particular element is present on the web page. This is a very powerful function in selenium as you can use it with a if condition.
In this example I am first checking if there is an element //td[@class='Selenium'] on the web page. If yes then I click the element //td[@class='Selenium'] and continue checking the other conditions.

if (selenium.IsElementPresent("//td[@class='Selenium']"))
{
selenium.Click("//td[@class='Selenium']");
selenium.WaitForPageToLoad("30000");
try
{
Assert.IsTrue(selenium.IsElementPresent("//td[@class='Selenium1']"));
}
catch (AssertionException)
{
writefail("Selenium RC is Present");
}
}
else
{
writefail("Selenium RC link does not exist");
}

Submitted by Pavandeep Puddupakkam

By Pavandeep Puddupakkam on July 21, 2010 | Selenium RC | 1 comment
Tags: , ,