【发布时间】:2021-06-14 06:39:58
【问题描述】:
我正在尝试抓取网页以获取联系电话,一切正常,但我想在“--headless”模式下运行脚本,但是当我使用此选项时,我遇到了错误。 但是当我禁用 --headless 模式时它可以工作。
我的代码:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
#options.add_argument('--disable-gpu') # maybe needed if running on Windows.
driver = webdriver.Chrome(executable_path='/home/dobuyme/Desktop/chromedriver', options=chrome_options)
print("Loading Page...")
driver.get('https://www.example.com')
time.sleep(5)
show_number_btn_xpath = '//span[contains(text(),"Show number")]'
sk = driver.find_element_by_xpath(show_number_btn_xpath)
sk.click()
soup = BeautifulSoup(driver.page_source,"html.parser")
title = soup.find("span", {"class": ["_truncate_multilines multiline_truncation"]}).get_text()
app = soup.find("div", {"class": ["_top_row-destop text-left"]}).find("h2").get_text().strip()
driver.quit()
contact = soup.find("div", {"class": ["_user_contact"]}).find("p", {"class": ["_call text-left"]}).get_text()
print(contact)
错误:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
如何在 --headless 模式下运行此脚本?
谢谢
【问题讨论】:
-
哪一行给你这个错误?是
show_number_btn_xpath = '//span[contains(text(),"Show number")]'吗? -
@cruisepandey 他只是想获得一些分数。这正是他在上一个问题中提出的问题,我在那里回答了他。没关系:)
-
@Prophet :在 SO 获得更多声誉是一项长期投资。这样做一段时间后,他会受够了。这是一场公平的比赛。
-
不完全是....
-
@cruisepandey 当然可以,但这应该以公平的方式完成
标签: python selenium selenium-webdriver