2015-09-30 87 views
2

我正在通過selenium的實踐,其中xpath在firefox xpath extesion中驗證,但不是在python硒中驗證。在硒中運行Xpath的文本()

例如去here和應用的XPath //span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]它工作在Firefox擴展,但不是在表達硒像driver.find_elements_by_xpath("//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]")

例外,我現在所面臨

Traceback (most recent call last): 
     Debug Probe, prompt 17, line 1 
     File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 270, in find_elements_by_xpath 
     return self.find_elements(by=By.XPATH, value=xpath) 
     File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_elements 
     {'using': by, 'value': value})['value'] 
     File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute 
     self.error_handler.check_response(response) 
     File "C:\Python27\ArcGIS10.3\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response 
     raise exception_class(message, screen, stacktrace) 
    selenium.common.exceptions.InvalidSelectorException: Message: The given selector //span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br] is either invalid or does not result in a WebElement. The following error occurred: 
    InvalidSelectorError: The result of the xpath expression "//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]" is: [object Text]. It should be an element. 

任何幫助是值得歡迎的。

回答

1

對於多返回記錄:

driver.get("http://www.hotleathers.com/Front-Printed-T-Shirts-C1232.aspx?s=OrderBy%20ASC&&v=all") 

returnText = [] 
returnText = self.driver.execute_script("var iterator = document.evaluate(\"//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]\", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); var arrayXpath = new Array(); var thisNode = iterator.iterateNext(); while (thisNode) {arrayXpath.push(thisNode.textContent); thisNode = iterator.iterateNext(); } return arrayXpath;") 

for item in returnText: 
    print item 

純JavaScript代碼:

var iterator = document.evaluate('//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]", documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); 

try { 
    var thisNode = iterator.iterateNext(); 

    while (thisNode) { 
    alert(thisNode.textContent); 
    thisNode = iterator.iterateNext(); 
    } 
} 
catch (e) { 
    dump('Error: Document tree modified during iteration ' + e); 
} 
+0

這個js用於填充元素的數組無法用於網站https://www.google.com/ncr#q=chiropractors%2BNew+York,+NY&rflfq=1&rlha=0&tbm=lcl它爲xpath提供了截斷的商業名稱' // div [@class ='_ pl _ki']/descendant-or-self :: text()[1]''''只喜歡'百老匯'而不是'百老匯整脊療養' – SIslam

+0

您選擇的索引[1] –

+0

啊! '[2]'選擇評論,例如'4 reviews etc.''但事實是,''之後的所有文字都被排除在前面提到的'Chiropractic&Wellness'之外! – SIslam

1

Selenium僅評估web元素。 text()函數返回一個對象。解決方法是在javascript函數中執行xpath並將其值返回到變量中。爲前,爲蟒蛇:

returnText = [] 
returnText = driver.execute_script("return document.evaluate(\"//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]\", document, null, XPathResult.STRING_TYPE, null).stringValue;") 

for item in returnText: 
    print item 
+0

上面的腳本返回一個值,但是是有列表中的任何選項。 – SIslam

+0

你的意思是選擇選項列表? –

+0

我的意思是獲取滿足該xpath的該頁面中的所有html元素(請參閱問題)。 – SIslam