2017-04-13 60 views
0

我有以下HTML中我嘗試使用紅寶石硒網絡驅動器紅寶石硒網絡驅動器:如何找到與XPath DIV ID和CSS類特定元素

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

我試圖找到svg元素測試使用如下任一給定div ID在Ruby中的硒網絡驅動器

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工作,有人可以幫我解決這個問題嗎?

+0

您正在使用無效的CSS選擇器。你可以嘗試這一個,'div.container-fluid.container-results> div.result-row [id] svg' –

+0

@SudharsanSelvaraj仍然相同'無效的選擇器:指定一個無效或非法的選擇器***元素信息:{Using = css selector,value = div.container-fluid.container-results> div.result-row [0] svg}(org.openqa.selenium.InvalidSelectorException)' – RanPaul

+0

使用我提到的確切定位器。不要將'result-row [id]'替換爲'result-row [0]' –

回答

1

我試圖找到任何給定的DIV ID

的SVG元素。如果這是你想要的,你可以試試這個:

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

我想找到'div class =「result-row」內的所有'svg'元素, '可以有多個'svg'元素,在這種情況下只有一個.. – RanPaul

+0

我已經更新了我的答案,而不是find_element,你使用find_elements –

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對於硒而言是無效的,即使這對於瀏覽器也是有效的。

您可以使用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") 
1

您指定很多的層次在CSS選擇器。從你發佈的HTML中,它看起來並不像你需要的全部。在CSS選擇器中使用>指定使用空格表示任何後代的子項。 DIV的任何SVG後裔都應該有效。你應該能夠使用這個

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