user123
user123

Reputation: 27

Linktext in selenium webdriver

Is it possible to use linkText locator in this code

I used driver.findElement(By.linkText("welcome")).click();

But it didn't work. Please help....

<div class="back-to">
<a class="button blue" href="javascript:history.back()">welcome</a>
</div>

Upvotes: 0

Views: 477

Answers (2)

Subh
Subh

Reputation: 4424

The linkText should work in this case. Or else try the below alternatives(and please provide sufficient implicit timeout to give selenium sufficent time to detect the element):

1. Using xpath, to click on the element 'a' with exact innerHTML/text as 'welcome':

driver.findElement(By.xpath("//a[.='welcome']")).click();

2- Using JavascriptExecutor to click on the element with exact innerHTML/text as 'welcome':

((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//a[.='welcome']")));

3- Using partialLinkText to click on the link with partial text 'welcome'

driver.findElement(By.partialLinkText("welcome")).click();

Upvotes: 1

Nima Soroush
Nima Soroush

Reputation: 12814

This Should be enough:

driver.find_element_by_xpath('//a[@class="button blue"]').click();

Upvotes: 0

Related Questions