如何在Selenium的无头模式下发送文本到电子邮件字段时避免点击拦截

huangapple go评论101阅读模式
英文:

How to avoid click intercept while sending text to email field using Selenium in headless mode

问题

我想连接网站。我编写了以下代码:

  1. from time import sleep
  2. from fake_useragent import UserAgent
  3. from selenium.webdriver.support.ui import WebDriverWait as W
  4. from selenium.webdriver.support import expected_conditions as E
  5. from selenium import webdriver
  6. options = webdriver.ChromeOptions()
  7. options.add_argument("--start-maximized")
  8. options.add_argument('--no-sandbox')
  9. options.add_argument('--headless')
  10. user_agent = UserAgent().random
  11. options.add_argument(f'user-agent={self.user_agent}')
  12. options.add_argument('--disable-infobars')
  13. options.add_argument('--disable-dev-shm-usage')
  14. driver = webdriver.Chrome(driver_path, options=self.options)
  15. wait_time = 10
  16. wait_variable = W(self.driver, self.wait_time)
  17. driver.get("https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor")
  18. sleep(5)
  19. email_holder = wait_variable.until(E.presence_of_element_located((By.ID, 'email-label')))
  20. # 这部分不起作用
  21. # 我尝试过聚焦、点击,但都不起作用。
  22. # 看起来另一个元素接收到了点击
  23. # email_holder.click()
  24. email_holder.send_keys("email")
英文:

I want to connect website.
I write the following code:

  1. from time import sleep
  2. from fake_useragent import UserAgent
  3. from selenium.webdriver.support.ui import WebDriverWait as W
  4. from selenium.webdriver.support import expected_conditions as E
  5. from selenium import webdriver
  6. options = webdriver.ChromeOptions()
  7. options.add_argument("--start-maximized")
  8. options.add_argument('--no-sandbox')
  9. options.add_argument('--headless')
  10. user_agent = UserAgent().random
  11. options.add_argument(f'user-agent={self.user_agent}')
  12. options.add_argument('--disable-infobars')
  13. options.add_argument('--disable-dev-shm-usage')
  14. driver = webdriver.Chrome(driver_path, options=self.options)
  15. wait_time = 10
  16. wait_variable = W(self.driver, self.wait_time)
  17. driver.get("https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor")
  18. sleep(5)
  19. email_holder = wait_variable.until(E.presence_of_element_located((By.ID, 'email-label')))
  20. # this does not work
  21. # I tried to focus on, click on but nothing is working.
  22. # it looks that another element receive the click
  23. # email_holder.click()
  24. email_holder.send_keys("email")

My question is how to focus and send text to email_holder ?

答案1

得分: 1

Sure, here is the translated code part:

  1. [![enter image description here][1]][1]
  2. 应该使用 `input` 元素来插入值而不是您所定位的 `label` 元素
  3. 使用 `element_to_be_clickable()` 而不是 `presence_of_element_located()`
  4. email_holder = wait_variable.until(E.element_to_be_clickable((By.ID, 'email')))
  5. email_holder.send_keys("email")
  6. [1]: https://i.stack.imgur.com/HYdi1.png
  7. 如果仍然存在相同的问题那么设置窗口大小
  8. options.add_argument("--window-size=1920,1080")
英文:

如何在Selenium的无头模式下发送文本到电子邮件字段时避免点击拦截

It should be input element to insert the value Not the label element that you have targeted..

Use element_to_be_clickable() instead of presence_of_element_located()

  1. email_holder = wait_variable.until(E.element_to_be_clickable((By.ID, 'email')))
  2. email_holder.send_keys("email")

if you still have the same issue, then set the window-size

  1. options.add_argument("--window-size=1920,1080")

答案2

得分: 1

翻译好的部分如下:

  • 使用 CSS_SELECTOR:
  1. options = Options()
  2. options.headless = True
  3. options.add_argument("start-maximized")
  4. s = Service('C:\\BrowserDrivers\\chromedriver.exe')
  5. driver = webdriver.Chrome(service=s, options=options)
  6. driver.get('https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor')
  7. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email"))).send_keys("LearnToGrow@stackoverflow.com")
  8. driver.save_screenshot("email.png")
  • 使用 XPATH:
  1. options = Options()
  2. options.headless = True
  3. options.add_argument("start-maximized")
  4. s = Service('C:\\BrowserDrivers\\chromedriver.exe')
  5. driver = webdriver.Chrome(service=s, options=options)
  6. driver.get('https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor')
  7. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='email']"))).send_keys("LearnToGrow@stackoverflow.com")
  8. driver.save_screenshot("email.png")
  • 注意: 您需要添加以下导入:
  1. from selenium.webdriver.support.ui import WebDriverWait
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  • 浏览器快照:

如何在Selenium的无头模式下发送文本到电子邮件字段时避免点击拦截

英文:

To send a character sequence to the Email Address field within the loginpage you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    1. options = Options()
    2. options.headless = True
    3. options.add_argument("start-maximized")
    4. s = Service('C:\\BrowserDrivers\\chromedriver.exe')
    5. driver = webdriver.Chrome(service=s, options=options)
    6. driver.get('https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor')
    7. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email"))).send_keys("LearnToGrow@stackoverflow.com")
    8. driver.save_screenshot("email.png")
  • Using XPATH:

    1. options = Options()
    2. options.headless = True
    3. options.add_argument("start-maximized")
    4. s = Service('C:\\BrowserDrivers\\chromedriver.exe')
    5. driver = webdriver.Chrome(service=s, options=options)
    6. driver.get('https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor')
    7. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='email']"))).send_keys("LearnToGrow@stackoverflow.com")
    8. driver.save_screenshot("email.png")
  • Note: You have to add the following imports :

    1. from selenium.webdriver.support.ui import WebDriverWait
    2. from selenium.webdriver.common.by import By
    3. from selenium.webdriver.support import expected_conditions as EC
  • Browser snapshot:

如何在Selenium的无头模式下发送文本到电子邮件字段时避免点击拦截

huangapple
  • 本文由 发表于 2023年2月7日 02:09:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定