Stas Mackarow
Stas Mackarow

Reputation: 195

How to find element by Full xPath in Selenium PhantomJS

I want to find element by xPath that i just copied from browser HTML viewer. Is there any good way to use it? How can i locate this element?

For example this:

1)HTML

<span class="fifth-star star-common" data-star-index="5" jsaction="click:JdtzLb;mouseenter:UYGLjf"> </span>

xPath

/html/body/div[9]/div/div/div/div[1]/div/div/div[4]/div/div[2]/div[2]/div[4]/div/div/div[1]/div[1]/span[5]

2) HTML

<a class="actionlink" href="javascript:CCommentThread.DeleteComment( 'PublishedFile_Public_76561198045856086_852822766_0', '133257324794187128' );">Удалить</a>

xPath

//*[@id="comment"]/div[2]/div[1]/a[2]

I tried this way

driver.findElement(By.xpath("/html/body/div[9]/div/div/div/div[1]/div/div/div[4]/div/div[2]/div[2]/div[4]/div/div/div[1]/div[1]/span[5]"));

And this

driver.findElement(By.xpath("//*[@id=\"comment\"]/div[2]/div[1]/a[2]"));

Upvotes: 0

Views: 5791

Answers (1)

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

Use single quotes in the XPATH instead of escaping using \

try this way:

driver.findElement(By.xpath("//*[@id='comment']/div[2]/div[1]/a[2]"));

please share block code, in case of more efficient XPATH.


Other ways:

driver.findElement(By.xpath("//a[starts-with(@href,'javascript:CCommentThread.DeleteComment')]"));

Upvotes: 1

Related Questions