gpt4 book ai didi

javascript - 使用 Selenium 修改 Javascript Navigator 对象

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:53 27 4
gpt4 key购买 nike

我尝试使用 Selenium(使用 geckodriver)访问一个站点,它说我被阻止了,但我可以使用 Firefox 浏览器手动访问它。所以我比较了我的 fingerpirnt 的组件,唯一的区别是当我使用 Selenium 时,在 Navigator 对象中“webdriver”被设置为“true”。我尝试运行这段代码:

from selenium import webdriver

from selenium.webdriver.firefox.options import Options
firefox_binary = '/usr/bin/firefox'
options = Options()
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().FIREFOX
# caps["pageLoadStrategy"] = "normal" # complete
caps["pageLoadStrategy"] = "eager" # interactive
injected_javascript=("Object.defineProperty(navigator, 'webdriver', { value: 'false' })")
driver = webdriver.Firefox(executable_path=r'/home/kkkk/ggecko/geckodriver', firefox_binary=firefox_binary)
driver.get('https://auth.citromail.hu/regisztracio/')

driver.execute_async_script(injected_javascript)

但它刚刚加载页面时“webdriver”仍设置为“true”,然后返回此消息:

Traceback (most recent call last):
File "/home/kkkk/driverr.py", line 14, in <module>
driver.execute_async_script(injected_javascript)
File "/home/kkkk/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 652, in execute_async_script
'args': converted_args})['value']
File "/home/kkkk/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/home/kkkk/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out

我哪里做错了,或者是否有其他方法可以做到这一点?

最佳答案

引用这个问题: Selenium webdriver: firefox headless inject javascript to modify browser property

它提供了一个有用的途径。

这是代码:

import os
from selenium import webdriver
options=webdriver.FirefoxOptions()
options.set_headless(True)
driver=webdriver.Firefox(options=options)
# solution found here https://stackoverflow.com/questions/17385779/how-do-i-load-a-javascript-file-into-the-dom-using-selenium
driver.execute_script("var s=window.document.createElement('script'); s.src='javascriptFirefox.js';window.document.head.appendChild(s);")
driver.get('https://auth.citromail.hu/regisztracio/')

Javascript 文件 javascriptFirefox.js

// overwrite the `languages` property to use a custom getter
const setProperty = () => {
Object.defineProperty(navigator, "languages", {
get: function() {
return ["en-US", "en", "es"];
}
});

// Overwrite the `plugins` property to use a custom getter.
Object.defineProperty(navigator, 'plugins', {
get: () => [1, 2, 3, 4, 5],
});

// Pass the Webdriver test
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
callback();
};
setProperty();

关于javascript - 使用 Selenium 修改 Javascript Navigator 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50573455/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com