user3836485
user3836485

Reputation: 73

Selenium Webdriver Locators

I am automating a report submission process in WebDriver, the report has multiple pages and in all the page there will be Next button to proceed to next page.

The next button has a following locators,

<a class="buttonNext btn btn-green btnwidth" href="javascript:;" style="">Next</a>

The problem is all the Next buttons in the report has the same locators since the wizard contains the next, previous and submit button is common throughout the report and only the page content will change, but this panel remains the same throughout the report.

I can use the xpath of the Next button which is :

.//*[@id='wizard']/div[2]/a[3]

This xpath is same for all the places where the Next button is placed in the report.

My coding is given as below :-

driver.findElement(By.xpath("//div[@id='wizard']/div[2]/a[3]")).click();
Thread.sleep(3000);

((JavascriptExecutor)driver).executeScript("scroll(0,2500)");
driver.findElement(By.xpath("//div[@id='wizard']/div[2]/a[3]")).click();
driver.findElement(By.xpath("//div[@id='wizard']/div[2]/a[3]")).click();

//WebDriverWait wait2 = new WebDriverWait(driver, 10);
//WebElement element2 = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Next")));

 driver.findElement(By.id("SignatureDate")).click();
 driver.findElement(By.id("SignatureDate")).clear();
 driver.findElement(By.id("SignatureDate")).sendKeys("01/09/2016");

WebDriver is idenfying the first Next button xpath and clicking it, but for next two pages again I'm providing the same xpath to access the Next button, but the selenium throws me the error message as :-

ElementNotVisibleexception:element not visible.

How can I differentiate the Next button to identify it for WebDriver?

Note : The report is opened in iframe & I'm working in iframe.

Upvotes: 2

Views: 350

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

You should try using By.cssSele‌​ctor() every time with using WebDriverWait after switching back from iframe to defaulContent() as below :-

//Do your stuff inside iframe

//Now switch back to default content
driver.switchTo().defaultContent();

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSele‌​ctor("a.buttonNext")‌​)).click()

Upvotes: 1

Priyanka Chaturvedi
Priyanka Chaturvedi

Reputation: 423

You can use Explicit Wait to allow the page to load completely before it tries to click the Next Button on the other page. Also, if using iFrames you can use the below:

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("FRAME_NAME"));

Upvotes: 1

Related Questions