【问题标题】:Why is the Selenium output different if run interactively vs in a Python script?如果以交互方式运行与在 Python 脚本中运行,为什么 Selenium 输出会有所不同?
【发布时间】:2021-09-13 18:28:21
【问题描述】:

我在 Python 3 中使用 Selenium 来获取使用 JavaScript 的站点的页面源。当我在 iPython shell 中以交互方式运行它时,它会按我的预期工作。但是,当以非交互方式执行完全相同的脚本时,页面源不会完全呈现(JavaScript 组件不会被呈现)。这可能是什么原因?我在完全相同的机器(无头 Linux 服务器)上运行完全相同的代码。

#!/usr/bin/python3

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

WINDOW_SIZE = "1920,1080"

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size={0}".format(WINDOW_SIZE))
chrome_options.add_argument("--no-sandbox")

service = Service('/usr/local/bin/chromedriver')
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get("https://www.stakingrewards.com/staking/?page=1&sort=rank_ASC")
src  = driver.page_source

# Check page source length
print(len(src))

# Quit all windows related to the driver instance
driver.quit()

iPython shell 的输出是 220101,这是预期的,而命令行执行脚本 ($ python script.py) 的输出是 38265。因此,当我从命令行调用脚本时,我没有有效地呈现 JavaScript 组件。为什么?!

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver ipython


    【解决方案1】:

    问题不在于以交互方式或作为脚本运行。

    在您的代码中,您实际上并没有给驱动程序任何时间来呈现所有元素,从而导致源代码不完整。只是碰巧以交互方式运行它比作为脚本运行它要快一点,从而导致页面源长度更大。但是,我能够使用Waits(大约 650k)获得更大的页面源长度。

    Waits 可用于等待所需元素可见/存在等。在您的情况下,我假设它是主表。 下面给出的代码等待表格可见,然后返回页面源代码。

    代码sn-p-

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import TimeoutException
    
    driver.get("https://www.stakingrewards.com/staking/?page=1&sort=rank_ASC")
    
    try:
    #waiting for table data to be visible
        delay=20 #20 second delay
        WebDriverWait(driver, delay).until(EC.visibility_of_element_located((By.CLASS_NAME, 'rt-tbody')))
        print(len(driver.page_source))
    
    #raises Exception if element is not visible within delay duration
    except TimeoutException:
        print("Timeout!!!")
    
    driver.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      相关资源
      最近更新 更多