使用硒进行网络抓取时如何获取文本值?

时间:2019-01-15 13:07:17

标签: python-3.x selenium web-scraping

我正在网页上抓取。 我得到了元素<span class="product_content_brand"> NikeLab </span> 在python3上使用硒。

from selenium import webdriver


browser= webdriver.Chrome("/home/desarrollo10/Downloads/
chromedriver_linux64/chromedriver")

browser.get("https://theurge.com.au/")
C=browser.find_element_by_tag_name("a").click()
time.sleep(0.5)
D=browser.find_element_by_class_name("tag-filters_clearall").click()

S=browser.find_elements_by_class_name("product_content")

for s in S:
    print(s.text)

我想从元素中获取带有“ product_content”类的文本,并且得到:

  

WebDriverException:消息:无法访问chrome       (会议信息:chrome = 71.0.3578.98)       (驱动程序信息:chromedriver = 2.44.609551       (5d576e9a44fe4c5b6a07e568f1ebc753f1214634),平台= Linux 4.15.0-43-       通用x86_64)

1 个答案:

答案 0 :(得分:0)

找到here的可能的解决方案,有关在启动Chrome时添加几个参数(no-sandbox,disable-setuid-sandbox):

chrome_options = Options()
#argument to switch off suid sandBox and no sandBox in Chrome 
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")

browser= webdriver.Chrome("/home/desarrollo10/Downloads/chromedriver_linux64/chromedriver", chrome_options=chrome_options)

然后:

我没有看到class = "tag-filters_clearall",而是看到class = "tag-filters_clear-all"

所以我认为您打算拥有:

D=browser.find_element_by_class_name("tag-filters_clear-all").click()

不是:

D=browser.find_element_by_class_name("tag-filters_clearall").click()