[英]Selenium find_element_by_class_name and find_element_by_css_selector not working
我正在抓取一個具有以下結構的網站:
<div class="cr__stores-list">
<div class="cr__stores-item">store1</div>
<a href="/stores/store1/">
<div class="cr__stores-item-b">
<div class="cr__container-name">
<h3 class="cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300">
Store Name
</h3>
</div>
</div>
</div>
<div class="cr__stores-item">store2</div>
...
...
<div class="cr__stores-item">store3</div>
<div class="cr__stores-item">store4</div>
</div>
"div class="cr__stores-list"" 中的每個 "div class="cr__stores-item"" 都具有完全相同的結構。 對於每個“div class="cr__stores-item"”,我正在嘗試抓取 url 和 h3 標簽內的商店名稱
到目前為止,我有以下代碼:
wd = webdriver.Chrome('chromedriver', options=options)
wd.get('stores_url')
element = WebDriverWait(wd, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "cr__stores-list")))
list_stores_tags = element.find_elements_by_class_name("cr__stores-item")
for store_tag in list_stores_tags:
# store_tmp = store_tag.find_element_by_class_name("cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300")
# store_tmp = store_tag.find_element_by_css_selector('h3.cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300')
store= store_tmp.text
url_tmp = store_tag.find_element_by_tag_name("a")
url = url_tmp.get_attribute('href')
評論的 2 行是我嘗試獲取商店名稱。 使用此代碼,我可以毫無問題地成功獲取每個商店的所有網址。 但是我無法獲得商店名稱。 find_element_by_class_name 和 find_element_by_css_selector 似乎都不起作用,因為在這兩種情況下我都會遇到異常:
消息:沒有這樣的元素:無法找到元素:{"method":"css selector","selector":".cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300"}(會話信息:headless chrome=87.0 .4280.88)
或者
消息:沒有這樣的元素:無法找到元素:{"method":"css selector","selector":"h3.cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300"}(會話信息:headless chrome= 87.0.4280.88)
我究竟做錯了什么? 歡迎大家提出意見。 謝謝!
您錯誤地使用find_element_by_css_selector
。 嘗試
store_tag.find_element_by_css_selector('h3.cr__container-name-title.cr__text--subtitle.cr__textColor--colorDark300')
注意點而不是空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.