Need xpath expression which contains break in the label

@FindBy( how = How.XPATH,
         using = "//div[@class='dynamic-main-content']
                     //div[label[text()='Number of At Fault Claim Free Years']]
                        /select" )

In the actual HTML there is a break in the label text. How should we write the XPATH?

Solution: Don't use [text()='the text'], use [normalize-space(.)='the text']. 

Using . rather than text() makes your code resilient to things such as comments (or element markup) appearing within the text you are matching.

 

How to work on a Radio Button in Selenium WebDriver.

The code below explains how t select a Radio button on any WebPage.

Because of Ajax on websites sometimes we need to reload the elements so we need to try and locate the element on the page so we use a for loop

 

public static RadioButton getRadio( WebDriver driver, String label, String locator )

{

RadioButton radio = null;

int i = 0;

while ( true )

{

i++ ;

try

{

radio = new RadioButton( label,

driver.findElement( By.xpath( locator ) ) );

break;

}

catch ( StaleElementReferenceException e )

{

sleep( 50 );

}

if ( i > 10 )

{

System.out.println( “[WARNING] Unable to find RadioButton 10 attempts” );

break;

}

}

return radio;

}

 

Page Factory Class sample for Automation using WebDriver

Page Factory class involves the following:

  • Defining all the HTML elements on the HTML Page
  • Getting XPATH Id’s of all the page elements
  • Writing Methods that involve interaction of the elements that can be used on the actual Test Class Page

Code Sample:

import org.junit.Assert;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
public class PageFactoryClass
{
@FindBy( how = How.ID, using = “gwt-debug-button” )
WebElement _button;
@FindBy( how = How.ID, using = “gwt-debug-resultTextBox” )
WebElement _resultTextBox;
@FindBy( how = How.ID, using = “gwt-debug-buttonWithConfirm” )
WebElement _buttonWithConfirm;
@FindBy( how = How.ID, using = “gwt-debug-confirmationResultTextBox” )
WebElement _confirmationResultTextBox;
@FindBy( how = How.ID, using = “gwt-debug-buttonWithAlert” )
private WebElement _buttonWithAlert;
@FindBy( how = How.ID, using = “gwt-debug-alertResultTextBox” )
WebElement _alertResultTextBox;
@FindBy( how = How.XPATH, using = “//div/a[text()='AnchorButton']” )
WebElement _anchorButtonLink;
public WebDriver _driver;
public ButtonTestPage( WebDriver driver )
{
_driver = driver;
PageFactory.initElements( new AjaxElementLocatorFactory( _driver, 15 ), this );
}
//Sample method that can be reused in the Test class
public void clickOnButtonHandlerFires()
{
_button.click();
Assert.assertEquals( “Button Clicked”, _resultTextBox.getValue() );
}
}
}
Return top

INFORMATION

Do not forget to leave a comment if you find the posts useful.
 
35352