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

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

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

问题

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

    from time import sleep
    from fake_useragent import UserAgent
    from selenium.webdriver.support.ui import WebDriverWait as W
    from selenium.webdriver.support import expected_conditions as E

    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_argument('--no-sandbox')
    options.add_argument('--headless')
    user_agent = UserAgent().random
    options.add_argument(f'user-agent={self.user_agent}')
    options.add_argument('--disable-infobars')
    options.add_argument('--disable-dev-shm-usage') 
    
    driver = webdriver.Chrome(driver_path, options=self.options) 
    wait_time = 10
    wait_variable = W(self.driver, self.wait_time)  
    driver.get("https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor")
    sleep(5)  
    email_holder = wait_variable.until(E.presence_of_element_located((By.ID, 'email-label')))
    # 这部分不起作用
    # 我尝试过聚焦、点击,但都不起作用。
    # 看起来另一个元素接收到了点击
    # email_holder.click()
    email_holder.send_keys("email")
英文:

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

from time import sleep
from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E


from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('--no-sandbox')
options.add_argument('--headless')
user_agent = UserAgent().random
options.add_argument(f'user-agent={self.user_agent}')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage') 

driver = webdriver.Chrome(driver_path, options=self.options) 
wait_time = 10
wait_variable = W(self.driver, self.wait_time)  
driver.get("https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor")
sleep(5)  
email_holder = wait_variable.until(E.presence_of_element_located((By.ID, 'email-label')))
# this does not work
# I tried to focus on, click on but nothing is working.
# it looks that another element receive the click
# email_holder.click()
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:

[![enter image description here][1]][1]

应该使用 `input` 元素来插入值而不是您所定位的 `label` 元素

使用 `element_to_be_clickable()` 而不是 `presence_of_element_located()`

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

[1]: https://i.stack.imgur.com/HYdi1.png

如果仍然存在相同的问题那么设置窗口大小

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()

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

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

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

答案2

得分: 1

翻译好的部分如下:

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

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

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

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    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:

确定