How to click on text in Selenium Webdriver 2.x

I can not click on the values โ€‹โ€‹below HTML through the selenium webdriver command click on Java.

Here is my HTML ... I need to click on PAAcctAcctRels, PAAcctActivityData, etc., as in HTML.

I tried using 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? Tell me.

+5
source share
3 answers

1 - For clicking on the text "PAAcctActivityData" , you can use the code below:

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

2 - For clicking on the text "PAAcctAddrEmail ", you can use the code below:

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

NOTE. - The above xpaths will find span elements with exact innerHTML / text as 'PAAcctActivityData' or 'PAAcctAddrEmail' respectively.

+3
source

By.linkText("PAAcctAcctRels") will not work because this link has more text (i.e. ' currently selected ') and the problem with your xpath is that it starts with . //

The following should work (I avoided using * for performance)

 By.xpath("//div[@id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a") 
0
source

Try using // [@id = 'primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0'] / div [1] / a / span as xpath. Remove the initial '.' and add '/ span' to the end.

0
source

Source: https://habr.com/ru/post/1210466/


All Articles