Ruby Selenium Web Drive:如何通过xpath div id和css class

时间:2017-04-13 06:43:53

标签: ruby selenium xpath

我有一个下面的html尝试使用ruby selenium web驱动程序进行测试

<div class="container-fluid container-results">
    <div class="result-row" id="0">
        <ul>
            <li class="process-status-column upload-success-icon">
                <svg class="glyphicon-upload-green" viewBox="0 0 100 100">
                    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#doc_upload"></use>
                </svg>
            </li>
            <li class="file-label-column"><span>validated.txt</span></li>
            <li class="client-company-column"><span>test-client</span></li>
            <li class="reference-column"><span>Ref001</span></li>
        </ul>
    </div>
    <div class="result-row" id="1">
        <ul>
            <li class="process-status-column upload-error-icon">
                <svg class="glyphicon-exclamation" viewBox="0 0 100 100">
                    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#exclamation"></use>
                </svg>
            </li>
            <li class="file-label-column"><span>Error.txt</span>
                <div class="attachment-row">This invoice contains an error</div>
            </li>
            <li class="client-company-column"><span>test-client</span></li>
            <li class="reference-column"><span>Ref002</span></li>            
        </ul>
    </div>   
    <div class="result-row" id="2">
        <ul>
            <li class="process-status-column upload-pending-icon">
                <svg class="glyphicon-spin" viewBox="0 0 100 100">
                    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#refresh"></use>
                </svg>
            </li>
            <li class="file-label-column"><span>Pending.txt</span></li>
            <li class="client-company-column"></li>
            <li class="reference-column"></li>            
        </ul>
    </div>
</div>

我正在尝试使用selenium web驱动程序在Ruby中找到任何给定svg id的div元素,如下所示

    rows = driver.find_elements(:css => "div.container-fluid.container-results >div\##{row_number.to_i-1} >ul >li >a >svg")

但是当我运行第1行的脚本

时,我会遇到异常
    invalid selector: An invalid or illegal selector was specified
  (Session info: chrome=57.0.2987.133)
  (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 25 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

Driver info: org.openqa.selenium.chrome.ChromeDriver

*** Element info: {Using=css selector, value=div.container-fluid.container-results >div#0 >ul >li >a >svg} (org.openqa.selenium.InvalidSelectorException)
[remote server] sun.reflect.GeneratedConstructorAccessor17():-1:in `newInstance'
[remote server] sun.reflect.DelegatingConstructorAccessorImpl():-1:in `newInstance'
[remote server] java.lang.reflect.Constructor():-1:in `newInstance'
[remote server] org.openqa.selenium.remote.ErrorHandler(ErrorHandler.java):206:in `createThrowable'
[remote server] org.openqa.selenium.remote.ErrorHandler(ErrorHandler.java):158:in `throwIfResponseFailed'
[remote server] org.openqa.selenium.remote.RemoteWebDriver(RemoteWebDriver.java):678:in `execute'
[remote server] org.openqa.selenium.remote.RemoteWebDriver(RemoteWebDriver.java):388:in `findElements'
[remote server] org.openqa.selenium.remote.RemoteWebDriver(RemoteWebDriver.java):496:in `findElementsByCssSelector'
[remote server] org.openqa.selenium.By$ByCssSelector(By.java):441:in `findElements'
[remote server] org.openqa.selenium.remote.RemoteWebDriver(RemoteWebDriver.java):351:in `findElements'
[remote server] sun.reflect.GeneratedMethodAccessor19():-1:in `invoke'
[remote server] sun.reflect.DelegatingMethodAccessorImpl():-1:in `invoke'
[remote server] java.lang.reflect.Method():-1:in `invoke'
[remote server] org.openqa.selenium.support.events.EventFiringWebDriver$2(EventFiringWebDriver.java):103:in `invoke'
[remote server] com.sun.proxy.$Proxy1():-1:in `findElements'
[remote server] org.openqa.selenium.support.events.EventFiringWebDriver(EventFiringWebDriver.java):177:in `findElements'
[remote server] org.openqa.selenium.remote.server.handler.FindElements(FindElements.java):50:in `call'
[remote server] org.openqa.selenium.remote.server.handler.FindElements(FindElements.java):1:in `call'
[remote server] java.util.concurrent.FutureTask():-1:in `run'
[remote server] org.openqa.selenium.remote.server.DefaultSession$1(DefaultSession.java):176:in `run'
[remote server] java.util.concurrent.ThreadPoolExecutor():-1:in `runWorker'
[remote server] java.util.concurrent.ThreadPoolExecutor$Worker():-1:in `run'
[remote server] java.lang.Thread():-1:in `run'

如果我在浏览器控制台中尝试运行$("div.container-fluid.container-results >div#0 >ul >li >a >svg"),我确实看到svg元素,但它不能用于Ruby,有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

  

我试图找到任何给定div id的svg元素

如果这是你想要的,你可以尝试这个:

driver.find_element(:id => "the_given_id").find_elements(:tag_name => "svg")

答案 1 :(得分:1)

根据Selenium documentation :css不是find_elements的有效选择器。

它指定:class, :class_name, :id, :link_text, :link, :partial_link_text, :name, :tag_name, :xpath为有效选择器。

:css选择器在许多情况下仍然有效,但它并不总是像您期望的那样工作。例如,div.container-fluid.container-results > div#0对于selenium是无效的,即使它对浏览器有效。

您可以使用xpath或其他一些选择器来获取svg。

如果你想使用css选择器找到它,你可以使用nokogiri,这是一个例子:

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

答案 2 :(得分:1)

您在CSS选择器中指定了很多级别。从您发布的HTML中看,您看起来并不需要它们。在CSS选择器中使用>指定一个子节点,其中使用空格表示任何后代。 SVG的任何DIV后代都应该有效。你应该可以使用这个

rows = driver.find_elements(:css => "div.container-fluid.container-results svg")