Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python html与无头chrome不同?_Python_Selenium_Google Chrome_Selenium Webdriver - Fatal编程技术网

Python html与无头chrome不同?

Python html与无头chrome不同?,python,selenium,google-chrome,selenium-webdriver,Python,Selenium,Google Chrome,Selenium Webdriver,问题 将Selenium与Chrome结合使用时,html在无头模式下是不同的。 详细信息 我一直在尝试使用Selenium导航到一个网站,我想在谷歌的登录身份验证后抓取这个网站。问题在于登录页面;当我使用headless模式时,html是不同的。没有无头模式,我可以输入我的用户名和密码,并登录到网站没有问题。它在headless模式下不起作用,所以当我检索html时,html的不同之处如下:没有headless 无头 (不要介意日语;它只是说“输入电子邮件”)我尝试过更改标记以发送凭据,但它只

问题
将Selenium与Chrome结合使用时,html在无头模式下是不同的。

详细信息
我一直在尝试使用Selenium导航到一个网站,我想在谷歌的登录身份验证后抓取这个网站。问题在于登录页面;当我使用headless模式时,html是不同的。没有无头模式,我可以输入我的用户名和密码,并登录到网站没有问题。它在headless模式下不起作用,所以当我检索html时,html的不同之处如下:
没有headless

无头

(不要介意日语;它只是说“输入电子邮件”)
我尝试过更改标记以发送凭据,但它只会将我带到一个包含不同语言列表的站点。

解决方案尝试了
除了更改标记之外,我还尝试了添加
用户代理
,但没有成功。它给了我一个不同的html,看起来像自动生成的谷歌页面。我还添加了一些选项(见下文),但没有任何帮助。

如果有一种方法可以使用与在无头模式下手动使用Chrome相同的html,那就太好了。我知道这可能是谷歌的事情,但有办法吗?提前感谢。

一些代码(Python)

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import chromedriver_binary
import requests
import time
username = "[REDACTED]"
username2 = "[REDACTED]"
password = "[REDACTED]"

url = "https://sites.google.com/[REDACTED]"

options = Options()
#options.binary_location = "[REDACTED]"
options.add_argument("--headless");
options.add_argument("--disable_gpu");
options.add_argument("--window-size=1920,1080");
#options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36")
driver = webdriver.Chrome("/Users/[REDACTED]", options=options)

driver.get(url)
print(driver.page_source)
n=0
while n < 5:
    try:
        driver.find_element_by_id("identifierID").send_keys(username2)
        driver.find_element_by_id("next").click()
        print("good")
        break
    except:
        print("no")
        n += 1
print(driver.current_url)
# WebDriverWait(driver, 10).until(EC.url_contains("https://accounts.google.com/signin/v2/challenge"))
print(driver.page_source)
driver.quit()
...
通过导入从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium.webdriver.support将预期的_条件导入为EC 从selenium.webdriver.chrome.options导入选项 从webdriver_manager.chrome导入ChromeDriverManager 导入chromedriver\u二进制文件 导入请求 导入时间 username=“[redact]” username2=“[redact]” password=“[redact]” url=”https://sites.google.com/[修订]” 选项=选项() #options.binary_location=“[redact]” 选项。添加_参数(“--headless”); 选项。添加_参数(“--disable_gpu”); options.add_参数(“--windowsize=19201080”); #选项。添加参数(“用户代理=Mozilla/5.0(Macintosh;英特尔Mac OS X 10_15_7)AppleWebKit/537.36(KHTML,如Gecko)Chrome/90.0.4430.93 Safari/537.36”) driver=webdriver.Chrome(“/Users/[redact]”,options=options) 获取驱动程序(url) 打印(驱动程序页\源) n=0 当n<5时: 尝试: 驱动程序。通过id(“identifierID”)查找元素。发送密钥(用户名2) 驱动程序。按\u id(“下一步”)查找\u元素。\u。单击() 打印(“好”) 打破 除: 打印(“否”) n+=1 打印(驱动程序。当前\u url) #WebDriverWait(驱动程序,10)。直到(EC.url_包含(“https://accounts.google.com/signin/v2/challenge")) 打印(驱动程序页\源) driver.quit() ... (请忽略凌乱的代码,仍在调试)