Python and Selenium xpath for selecting with multiple conditions

社会主义新天地 提交于 2020-07-03 02:42:10

问题


I have the following code in selenium but continue to get a syntax error. I'm trying to select an element based on multiple conditions.

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and [contains(@class, 'option')]]")$

Thanks for any help you can give.


回答1:


As per the xpath you have shared as follows :

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and [contains(@class, 'option')]]")$

You need to consider a few facts :

  • The multiple conditions for selecting the <div> tag can't be within nested []. Either you have to specify within one [] or within multiple []s.
  • The xpath shouldn't end with unwanted characters e.g $

Solution

You can rewrite the xpath in either of the following ways :

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and contains(@class, 'option')]")
# or
choices = driver.find_elements_by_xpath("//div[contains(.,'5')][contains(@class, 'option')]")


来源:https://stackoverflow.com/questions/49459491/python-and-selenium-xpath-for-selecting-with-multiple-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!