【发布时间】:2021-08-03 01:34:39
【问题描述】:
我正在使用带有 selenium 包的无头 Chrome。当我手动访问该网站并向下滚动时,它会加载更多的 itens,并且列表“nomes”会在下面显示的 while 循环中更新。当我使用 selenium 和有头浏览器时,它也可以工作。为什么页面没有加载无头?也许这无关紧要,但我也将 userAgent 从 ua.random 更改为 ua['Chrome']。
import fake_useragent
import selenium
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--incognito')
chrome_options.add_argument('--headless')
chrome_options.add_argument("--window-size=1920,1080")
userAgent = ua['Chrome']
chrome_options.add_argument(f'user-agent={userAgent}')
driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)
driver.get('https://www.website.com/')
nomes = driver.find_elements_by_css_selector('my.css')
iteracao = 0
if nomes:
while iteracao < 3:
iteracao += 1
nomes = driver.find_elements_by_css_selector('my.css')
driver.execute_script("arguments[0].scrollIntoView();", nomes[-1])
time.sleep(1)
wait(driver, 10).until(
wait_for_more_than_n_elements((By.CSS_SELECTOR, 'my.css'), len(nomes)))
哪里,我从here得到的,
class wait_for_more_than_n_elements(object):
def __init__(self, locator, count):
self.locator = locator
self.count = count
def __call__(self, driver):
try:
count = len(EC._find_elements(driver, self.locator))
return count >= self.count
except selexcept.StaleElementReferenceException:
return False
【问题讨论】:
标签: python selenium scroll webdriver load