首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium非常奇怪地处理“@”字符

Selenium非常奇怪地处理“@”字符
EN

Stack Overflow用户
提问于 2022-07-18 06:17:51
回答 2查看 98关注 0票数 1

我正在尝试使用标准的python中的Selenium,以及正常和无头浏览器模式下的undetected_chrome驱动程序。我注意到了一些奇怪的行为:

如果我将电子邮件地址发送到HTML输入字段,如“character”中的当前内容粘贴在“@character”中,则

  • 普通的驱动程序可以很好地处理在<>E 114普通浏览器模式中的send_keys()函数
    • 中的特定输入输入字段中的键。

代码语言:javascript
复制
    - 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
代码语言:javascript
复制
- 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?

  • How,我能用无头浏览器模式正确发送电子邮件地址吗?(由于机器人,我需要使用undetected_chrome驱动程序)

代码语言:javascript
复制
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)
代码语言:javascript
复制
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版本

代码语言:javascript
复制
selenium==4.3.0
undetected-chromedriver==3.1.5.post4
EN

回答 2

Stack Overflow用户

发布于 2022-07-19 11:00:23

不要使用自定义配置选项,先尝试一个更基本的变体,看看是否正确。然后找出是哪个选项组合导致了问题。

一个示例(顺便说一句,它确实工作正常)

代码语言:javascript
复制
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")
票数 2
EN

Stack Overflow用户

发布于 2022-07-18 15:31:20

这个抱怨(输入"@“粘贴剪贴板内容)不时出现在这里,但我从未见过明确的解决方案。我怀疑Windows &/或键盘驱动程序的特定语言版本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73018012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档