英文:
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")
英文:
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
- 浏览器快照:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论