Ruby Selenium Web Drive: How to find specific element by xpath div id and css class

后端 未结 3 390
北海茫月
北海茫月 2021-01-27 22:45

I have a below html which am trying to test using ruby selenium web driver

相关标签:
3条回答
  • 2021-01-27 23:19

    You are specifying a lot of levels in the CSS selector. From the HTML you posted, it doesn't look like you need them all. Using > in a CSS selector specifies a child where using a space signifies any descendant. Any SVG descendant of the DIV should work. You should be able to use just this

    rows = driver.find_elements(:css => "div.container-fluid.container-results svg")
    
    0 讨论(0)
  • 2021-01-27 23:20

    According to the Selenium documentation :css is not a valid selector for find_elements.

    It specified :class, :class_name, :id, :link_text, :link, :partial_link_text, :name, :tag_name, :xpath to be valid selectors.

    The :css selector do still work in many situations, however it does not always work as you would expect. For example would div.container-fluid.container-results > div#0 be invalid for selenium, even tho it is valid for the browsers.

    You could use xpath or some of the other selectors to get the svg.

    If you want to use the css selector to find it, you could use nokogiri, here is an example:

    selenium_driver = Selenium::WebDriver.for :chrome
    selenium_driver.get("http://stackoverflow.com/")
    
    # do whatever with selenium
    
    doc = Nokogiri::HTML(selenium_driver.page_source)
    doc.css("div.container-fluid.container-results >div##{row_number.to_i-1} >ul >li >a >svg")
    
    0 讨论(0)
  • 2021-01-27 23:29

    I am trying to find the svg element of any given div id

    If this is what you want, you can try this one:

    driver.find_element(:id => "the_given_id").find_elements(:tag_name => "svg")
    
    0 讨论(0)
提交回复
热议问题