1

I've been trying to click onto the hyperlink which leads to a popup on the screen. For some reason it is not working, currently the html for the hyperlink is this:

<a href="javascript:void(0)" class="operating-hours left">Select Hours</a>

Trying with this selenium code:

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.linkText("Select Hours"))).click(); 

Also tried:

driver.findElement(By.id("operating_hours")).click();

Interestingly I am not getting any error either, just that the hyperlink is not clicked. screenshot after running the script After running the script, that is the screenshot.

Whereas the script should have opened this popup window popup that should open after running the script @DebanjanB hope that clarifies the problem.

1
  • 1
    Please provide HTML code, and please format code to be more readable, please use code block, tnx Commented Jul 20, 2018 at 11:09

2 Answers 2

2

As per the HTML you have shared instead of linkText you can use either of the following approaches and Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.operating-hours.left"))).click();
    
  • xpath:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='operating-hours left'][contains(.,'Select Hours')]"))).click();
    
  • If you still unable to invoke click() on the desired element you can use Actions Class as follows:

    WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.operating-hours.left")));
    //WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='operating-hours left'][contains(.,'Select Hours')]")));
    new Actions(driver).moveToElement(element).click().build().perform();
    
  • As an alternative you can use executeScript() method from the JavascriptExecutor Interface as follows:

    WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.operating-hours.left")));
    //WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='operating-hours left'][contains(.,'Select Hours')]")));
    (JavascriptExecutor)driver.executeScript("arguments[0].click();", element);
    
Sign up to request clarification or add additional context in comments.

5 Comments

tried both but no improvement.. thanks anyways @debanjanb
@JaffarLone no improvement doesn't throws any light on whats wrong happening. Can you update the question with your current code trials and error you are seeing?
well i am attaching two screenshots, since i am not getting any error logs from testNG or Eclipse, thats all i've got.
@JaffarLone Checkout my answer update and let me know the status
the second option is working, popup is finally opening on the main screen. Thanks :)
1

Here are several examples to click on link via different selectors:

Click link by full text

WebElement element = driver.findElement(By.linkText("My Link"));
linkByText.click();

Click link by partial text

WebElement element = driver.findElement(By.partialLinkText("First"))
linkByPartialText.click();

Click link by text using XPath

WebElement element = driver.findElement(By.xpath("//a[text()='First']"));
linkByTextUsingXPath.click();

Click link by partial text using XPath

WebElement element = driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));
linkByPartialTextUsingXPath.click();

Also if needed use explicit wait:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));

or

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("some_link")));
element.click();

Hope this helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.