我有以下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工作,有人可以幫我解決這個問題嗎?
您正在使用無效的CSS選擇器。你可以嘗試這一個,'div.container-fluid.container-results> div.result-row [id] svg' –
@SudharsanSelvaraj仍然相同'無效的選擇器:指定一個無效或非法的選擇器***元素信息:{Using = css selector,value = div.container-fluid.container-results> div.result-row [0] svg}(org.openqa.selenium.InvalidSelectorException)' – RanPaul
使用我提到的確切定位器。不要將'result-row [id]'替換爲'result-row [0]' –