python - html 与无头 chrome 有什么不同?
问题描述
问题
当在 Chrome 中使用 Selenium 时,在无头模式下 html 是不同的。
详细信息
我一直在尝试使用 Selenium 导航到我想在 Google 登录身份验证后面抓取的网站。问题出在登录页面上;当我使用无头模式时,html 是不同的。没有无头模式,我可以输入我的用户名和密码并登录网站没有问题。它在无头模式下不起作用,所以当我检索 html 时,html 的不同如下:
没有无头
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="メールアドレスを入力してください" name="identifier" value="" autocapitalize="none" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="">
有无头
<input id="Email" type="email" value="" spellcheck="false" name="Email" placeholder="メールアドレスを入力してください" autofocus="">
(不要介意日语;它只是说“输入电子邮件”)
我尝试将标签更改为发送我的凭据,但它只会将我带到一个包含不同语言列表的站点。
尝试过的解决方案
除了更改标签之外,我尝试添加User Agent
但没有运气。它给了我一个不同的 html,看起来像自动生成的 Google 页面。我还添加了一些选项(见下文),但这并没有帮助。
如果有一种方法可以让我使用与在无头模式下手动使用 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()
...
(请忽略乱码,还在调试中)
解决方案
推荐阅读
- ld - 为什么递归 ld rpath 的应用程序不起作用?
- spring-boot - 嵌入 Vert.x 的 Spring Boot
- c - 在循环中使用 scanf() 扫描 CSV 文件在第一行和第二行开始之后停止
- python - 使用 Wand python 将 PDF 转换为图像
- opengl - 如何在带有 GPU 的 Google Compute Engine 上运行 Gazebo?
- asp.net-mvc - 如何在两个 React 组件之间切换到一个文件中?
- reporting-services - 每页的标题 SSRS
- java - Hikari 抛出错误 目标类 org.postgresql.ds.PGPoolingDataSource 上不存在属性 url
- c# - ASP.NET Core 中的多种分布式缓存
- python - 如何编写返回循环的函数?