我正在尝试使用标准的python中的Selenium,以及正常和无头浏览器模式下的undetected_chrome驱动程序。我注意到了一些奇怪的行为:
如果我将电子邮件地址发送到HTML输入字段,如“character”中的当前内容粘贴在“@character”中,则
send_keys()函数 - e.g.: if I have copied the string '123' for the last time, the entered email address will not be 'abc@xyz.com' but 'abc123xyz.com' which is obviously incorrect
- that's why I'm using a workaround, that I import pyperclip and put '@' character to the clipboard before the `send_keys()` function runs, to replace the '@' character with '@' character correctly- in **headless browser mode** if I enter an email address into a HTML input field, like 'abc@xyz.com' my workaround doesn't matter any more, the '@' character will be stripped from the email, and the 'abcxyz.com' string will be put into the field我认为发送一个“@”字符在默认情况下不应该那么困难。我在这里做错什么了吗?
问题
有人能解释一下这些奇怪的behaviors?
from selenium import webdriver
self.chrome_options = webdriver.ChromeOptions()
if self.headless:
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
prefs = {'download.default_directory' : os.path.realpath(self.download_dir_path)}
self.chrome_options.add_experimental_option('prefs', prefs)
self.driver = webdriver.Chrome(options=self.chrome_options)import undetected_chromedriver as uc
# workaround for problem with pasting text from the clipboard into '@' symbol when using send_keys()
import pyperclip
pyperclip.copy('@') # <---- THIS IS THE WORKAROUND FOR THE PASTING PROBLEM
self.chrome_options = uc.ChromeOptions()
if self.headless:
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.driver = uc.Chrome(options=self.chrome_options)
params = {
"behavior": "allow",
"downloadPath": os.path.realpath(self.download_dir_path)
}
self.driver.execute_cdp_cmd("Page.setDownloadBehavior", params)我使用的requirements.txt版本
selenium==4.3.0
undetected-chromedriver==3.1.5.post4发布于 2022-07-19 11:00:23
不要使用自定义配置选项,先尝试一个更基本的变体,看看是否正确。然后找出是哪个选项组合导致了问题。
一个示例(顺便说一句,它确实工作正常)
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
driver = uc.Chrome()
driver.execute_script("""
document.documentElement.innerHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html, body, main {
height: 100%;
}
main {
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<main>
<form>
<input type="text" name="text" />
<input type="email" name="email"/>
<input type="tel" name="tel"/>
</form>
</main>
</body>
</html>`
""")
driver.find_element(By.NAME, "text").send_keys("info@example.com")
driver.find_element(By.NAME, "email").send_keys("info@example.com")
driver.find_element(By.NAME, "tel").send_keys("info@example.com")发布于 2022-07-18 15:31:20
这个抱怨(输入"@“粘贴剪贴板内容)不时出现在这里,但我从未见过明确的解决方案。我怀疑Windows &/或键盘驱动程序的特定语言版本。
https://stackoverflow.com/questions/73018012
复制相似问题