Tony
Tony

Reputation: 117

Selenium click anchor link with class attribute

Can anyone help how to click the below link using selenium

<a class=”btn btn-primary btn-large” href="target-URL">Submit</a>

I tried using below options

  1. linkText
  2. partialLinkText
  3. CssSelector
  4. contains - logic which checks the URL text

Upvotes: 1

Views: 6318

Answers (4)

Prashant Dhage
Prashant Dhage

Reputation: 57

Give full xpath

driver.findElement(By.xpath("html/body/a").click();

Can try with tag name

driver.findElement(By.tagName("a").click();

Upvotes: 1

Kumar
Kumar

Reputation: 328

You can try:

driver.findElement(By.xpath("//a[contains@class,'btn '] and contains(@class, 'btn-large') and contains(text(), 'Submit')")).click()

Upvotes: 1

Saritha G
Saritha G

Reputation: 2608

Try with below xpath:

driver.findElement(By.xpath("//a[@href='target-URL']").click();

Upvotes: 0

alecxe
alecxe

Reputation: 473863

In theory, this is just:

driver.findElment(By.linkText("Submit")).click();

But, I'm pretty sure you've already tried that. Check if the element is in iframe/frame. If yes, you need to switch to it and only then find the link element:

driver.switchTo().frame("frame_name_or_id");

To switch back in the main context, use defaultContent():

driver.switchTo().defaultContent();

Upvotes: 0

Related Questions