Jeff
Jeff

Reputation: 8411

Click on link using selenium and webdriver

I have been trying to perform a selenium task on it:

In this page, there is a button which i have to click on it and then wait for 10 seconds. I did it like this: Naviagation to page:

base.driver.navigate().to("http://suvian.in/selenium/1.7button.html");

Click on button:

//base.driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div/h3[2]/a"));
        base.driver.findElement(By.linkText("Click Me"));

This step fails

Wait for 10 seconds:

TimeUnit.SECONDS.sleep(waitTime);

Questions:

1-it fails on clicking on the button. Although, i asked to find the link both with xpath, and text it cannot find it?

2-Is my solution correct for make a delay on webdriver's activity?

Upvotes: 0

Views: 782

Answers (1)

anshul Gupta
anshul Gupta

Reputation: 1270

  1. Try Below code for clicking on the "Click Me" button, tried on my local: driver.findElement(By.xpath("//div[contains(@class,'intro-message')]")).findElement(By.partialLinkText("Click Me")).click();

    Explanation for the above code : Thumb rule is try to go from the parent element of the DOM. In the above post, your parent element for the button is div class = intro-message . Once the parent element is located, then next find the child elements. In your case it was the button with link text 'Click Me'.

//base.driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div/h3[2]/a")); base.driver.findElement(By.linkText("Click Me"));

Also, the way you have written is not correct. This will fail in case more element are added in between like a new div or a new button. Try avoiding this.

  1. Yes for the current scenario, your way of making wait is right. But for other use case it might not be right to make your application wait explicitly.

Upvotes: 1

Related Questions