问题描述:

I'm using Python with webdriver

I'm trying to locate element with find element with xpath and get :"unexpected EOF while parsing (, line 1)"

When I try to locate it by finding element with class name, it works well.

The problem is that I can't use it since I have several classes with the same name.

Here is xpath:

//*[@id="j_id0:j_id5:j_id6:j_id36"]/div/div/div[2]/div[2]/div[2]/ul/li[1]/div/svg/g/g[1]/path[1]

here is the class name : st0

here is my code line:

ss = self.SELENIUM_DRIVER.find_element_by_xpath('//*[@id="j_id0:j_id5:j_id6:j_id36"]/div/div/div[2]/div[2]/div[2]/ul/li[1]/div/svg/g/g[1]/path[1]')

-Which does not work

ddd = self.SELENIUM_DRIVER.find_element_by_class_name('st0')

-Which works

This is the html:

Thanks in advance

网友答案:

The following query will look for a path with the parent tree of div/svg/g/g[1], with a class equalling st0 and at index [1]:

self.SELENIUM_DRIVER.find_element_by_xpath('//div/svg/g/g[1]/path[@class=st0][1]')

If the parent tree is unique from earlier on, it may be a good idea to make the query shorter, e.g.:

self.SELENIUM_DRIVER.find_element_by_xpath('//g/g[1]/path[@class=st0][1]')
网友答案:

I think this is the problem with your provided Id j_id0:j_id5:j_id6:j_id36 and as you are saying class name is same for some other elements as well, So you should try using xpath by passing index as below :-

self.SELENIUM_DRIVER.find_element_by_xpath('(//path[@class=st0])[1]')

Now you can locate your desire element by just changing index value.

Hope it works...:)

相关阅读:
Top