Reputation: 73
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.
Upvotes: 1
Views: 698
Reputation: 1481
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,
Upvotes: 1
Reputation: 193338
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);
Upvotes: 2