Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Click on a Text in Selenium Webdriver 2.x

I am not able to click on the below HTML values through selenium webdriver click command through Java.

Here's my HTML...I have to click on PAAcctAcctRels, PAAcctActivityData, etc. as in the HTML.

I tried with LinkText (driver.findElement(By.linkText("PAAcctAcctRels")).click();) and xpath (driver.findElement(By.xpath(".//[@id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")).click();)

<div id="primaryNavLevel2Z6_0G5A11K0KGF200AIUB98T20G52" class="dropdown_1columns">
  <div class="col_1">
    <a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20G53">
      <strong>
        <span lang="en" dir="ltr">
          PAAcctAcctRels
          <span class="wpthemeAccess"> currently selected</span>
        </span>
      </strong>
    </a>
  </div>
  <div class="col_1">
    <a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GD4">
      <span lang="en" dir="ltr">PAAcctActivityData</span>
    </a>
  </div>
  <div class="col_1">
    <a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GT1">
      <span lang="en" dir="ltr">PAAcctAddrEmail</span>
    </a>
  </div>

Is there any other way to do this..please let me know.

like image 971
Ganesh Kumar Ravichandran Avatar asked Jan 06 '15 12:01

Ganesh Kumar Ravichandran


People also ask

How do you click text in Selenium?

We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method.

How do I enter text and click enter in Selenium?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.


1 Answers

1- For Clicking on text 'PAAcctActivityData', you can use the below code:

driver.findElement(By.xpath("//span[.='PAAcctActivityData']")).click();

2- For Clicking on text 'PAAcctAddrEmail', you can use the below code:

driver.findElement(By.xpath("//span[.='PAAcctAddrEmail']")).click();

NOTE:- The above xpaths will locate thespan elements with exact innerHTML/text as 'PAAcctActivityData' or 'PAAcctAddrEmail', respectively.

like image 157
Subh Avatar answered Sep 27 '22 23:09

Subh