【发布时间】:2021-10-05 08:27:18
【问题描述】:
问题可能是内存使用。页面开始变得非常慢,有时会出现以下错误消息
from bs4 import BeautifulSoup
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
# Set some Selenium Options
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# Webdriver
wd = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)
# URL
url = 'https://www.techpilot.de/zulieferer-suchen?laserschneiden'
# Load URL
wd.get(url)
# Get HTML
soup = BeautifulSoup(wd.page_source, 'html.parser')
wd.fullscreen_window()
wait = WebDriverWait(wd, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#bodyJSP #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#efficientSearchIframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".hideFunctionalScrollbar #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
#wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".fancyCompLabel")))
roaster=wd.find_element_by_xpath('//*[@id="resultTypeRaster"]')
ActionChains(wd).click(roaster).perform()
#use keys to get where the button is
html = wd.find_element_by_tag_name('html')
c=2
for i in range(100):
html.send_keys(Keys.END)
time.sleep(1)
html.send_keys(Keys.END)
time.sleep(1)
html.send_keys(Keys.ARROW_UP)
try:
wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[@id='resultPane']/div["+str(c)+"]/span")))
loadButton=wd.find_element_by_xpath("//*[@id='resultPane']/div["+str(c)+"]/span")
loadButton.click()
except TimeoutException or ElementClickInterceptedException:
break
time.sleep(1)
c+=1
wd.close
这是我浏览过的一些类似问题的链接 我尝试添加选项,但它不会工作。其他一些技巧真的让我很困惑,所以我希望有人可以在这里帮助我(我对编码很陌生)
这是我浏览的链接
selenium.WebDriverException: unknown error: session deleted because of page crash from tab crashed
python linux selenium: chrome not reachable
只是为了澄清该程序的目标是获取所有配置文件的列表并从中刮取东西,这就是为什么这部分程序首先加载整个页面以获取所有这些链接(afaik 我不能只用它们bsoup 因为 javascript)所以我没有任何解决方法 非常感谢!
【问题讨论】:
-
您是要打开所有列出的配置文件,还是只打开卡片上可见的内容?
-
我试图打开网站上的所有配置文件(这就是为什么我指示 selenium 单击加载更多按钮并向下滚动,因为在加载之前找不到链接)希望能回答你的问题跨度>
-
确实如此。你的情况对于初学者来说有点高级。我会稍微修改一下你的代码并稍后发布。
-
非常感谢您的帮助:) 如果您能解释一下您所做的更改以便我能够知道我的错误是什么/我可以从中吸取教训,我将不胜感激。 (只是为了澄清我试图获得的输出是每个配置文件的链接列表)
-
听起来他们一直在加载新内容而不删除旧内容……在 DOM 加载足够多之后,浏览器会崩溃。 (可能是由于 JS 框架过载)
标签: python selenium selenium-webdriver out-of-memory