Home > database >  Selenium handles '@' character very strangely
Selenium handles '@' character very strangely

Time:07-20

I'm trying to use Selenium in python with standard and with undetected_chrome drivers in normal and headless browser modes. I've noticed some strange behaviors:

  • normal chrome driver works fine with special inputs while sending keys into HTML input field with send_keys() function
  • undetected_chrome driver does not handle the special inputs very well with send_keys() function
    • in normal browser mode if I send an email address into a HTML input field, like '[email protected]' the current content from the clipboard is pasted in place of '@' character

      • e.g.: if I have copied the string '123' for the last time, the entered email address will not be '[email protected]' 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 '[email protected]' 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

I think sending a '@' character shouldn't be that hard by default. Am I doing something wrong here?

Questions:

  • Can anyone explain these strange behaviors?
  • How could I send an email address correctly with headless browser mode? (I need to use undetected_chrome driver because of bots)
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)

The versions I'm using requirements.txt:

selenium==4.3.0
undetected-chromedriver==3.1.5.post4

CodePudding user response:

Instead of using custom configuration options, try a more basic variant first and see if works correctly. And then figure out which option combination is causing the issue.

An example (which does work correctly by the way)

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("[email protected]")
driver.find_element(By.NAME, "email").send_keys("[email protected]")
driver.find_element(By.NAME, "tel").send_keys("[email protected]")

CodePudding user response:

This complaint (entering "@" pastes clipboard contents) has come up here from time to time but I've never seen a definitive solution. I'd suspect a particular language version of Windows &/or keyboard driver.

  • Related