Python and Selenium xpath for selecting with multiple conditions

后端 未结 1 650
忘了有多久
忘了有多久 2021-01-12 10:46

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.fin         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 11:23

    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
      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')]")
    

    0 讨论(0)
提交回复
热议问题