2017-03-03 59 views
2

我在python中使用硒刮一個網站。 xpath能夠找到包含搜索結果的20個元素。但是,內容僅適用於前6個元素,其餘內容爲空字符串。這是硒刮後返回空字符串前幾個元素

中的XPath使用的結果的所有網頁真:

results = driver.find_elements_by_xpath("//li[contains(@class, 'search-result search-result__occluded-item ember-view')]") 

的XPath發現

enter image description here

文本結果裏面鉻20元

[tt.text for tt in results] 

匿名輸出:

['Abcddwedwada', 
'Asefdasdfaca', 
'Asdaafcascac', 
'Asdadaacjkhi', 
'Sfskjfbsfvbkd', 
'Fjsbfksjnsvas', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
'', 
''] 

我試過提取20個元素的id並使用driver.find_element_by_id,但仍然在前6個元素後面得到空字符串。

+0

你可以分享網頁鏈接? – Andersson

+0

https://www.linkedin.com/search/results/people/?keywords=Python&origin=SUGGESTION&suggestedEntities=SKILL – mrbot

回答

0

試試這個,

[str(tt.text) for tt in results if str(tt.text) !=''] 

OR

[tt.text for tt in results if len(tt.text) > 0] 
+0

這將過濾掉空字符串的結果 – mrbot

+0

@mrbot空字符串''的類型是什麼? unicode或字符串? –

+0

空字符串的類型是'str' – mrbot

0

我可以假設,這樣的結果的原因如下:當你打開的頁面有20項(在<ul><li>元素)但只顯示6個內容。向下滾動顯示其他元素的內容 - 從XHR請求動態生成的14個條目的內容。

所以,你可能需要執行列表中向下滾動到最後一個元素:

from selenium.webdriver.support.ui import WebDriverWait as wait 

wait(driver, 10).until(lambda x: len(driver.find_elements_by_xpath("//li[contains(@class, 'search-result search-result__occluded-item ember-view') and not(text()='')]")) == 20) 
results = driver.find_elements_by_xpath("//li[contains(@class, 'search-result search-result__occluded-item ember-view')]") 
results[-1].location_once_scrolled_into_view 
[tt.text for tt in results] 

嘗試,讓我知道結果

+0

它沒有工作。我想到了這一點,並嘗試:'driver.execute_script(「window.scrollTo(0,Y);」)' – mrbot

+0

使用'pyvirtualdisplay'有什麼用? – mrbot

+0

所有的20個元素都返回'True' for'is_displayed()' – mrbot