【问题标题】:How get the text values when webscraping with selenium?使用硒进行网页抓取时如何获取文本值?
【发布时间】:2019-01-15 13:07:17
【问题描述】:

我正在抓取网页。 我得到元素<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)

【问题讨论】:

  • Chrome 能打开吗?
  • 你确定 chrome 在“/home/desarrollo10/Downloads/chromedriver_linux64/chromedriver”吗?
  • 是的。页面打开,我可以观看。如果我写类型(S),结果是“列表”

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


【解决方案1】:

尝试在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()  

【讨论】:

  • 这很有道理...但是为什么会出现错误WebDriverException: Message: chrome not reachable
  • 谢谢,你是对的。我已完成此更正,但我无法获得属性文本。
猜你喜欢
  • 1970-01-01
  • 2021-10-27
  • 2023-02-02
  • 2023-04-02
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 2020-03-27
  • 2021-04-08
相关资源
最近更新 更多