无法使用python Selenium定位send_keys的表单控件对象

c3frrgcw  于 2023-01-13  发布在  Python
关注(0)|答案(1)|浏览(179)

我正在尝试使用以下脚本浏览日程安排网站,以便最终自动填充日程安排:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

# Create a Chrome webdriver
driver = webdriver.Chrome(r'C:\Users\chromedriver_win32\chromedriver.exe')

# Navigate to https://www.qgenda.com/
driver.get('https://www.qgenda.com/')

# Wait for the page to load
driver.implicitly_wait(5) # 5 seconds

# You can now interact with the page using the webdriver
# Locate the sign in button
sign_in_button = driver.find_element(By.XPATH,'/html/body/div[1]/div/header[3]/div/div[3]/div/div/div/div/a')

# Click the sign in button
sign_in_button.click()

# Find the input element
input_email = driver.find_element(By.XPATH,'//*[@id="Input_Email"]')

# Send text 
input_email.send_keys('Josh')

然而,我似乎找不到Input_Email对象。我已经尝试了所有有意义的Xpath和Id,也尝试了等待,直到对象是可点击的,但没有运气。真的很感激一些指导。
我期待Selenium找到html对象表单框并传入文本,但我得到了一个错误:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="Input_Email"]"},即使Xpath确实存在。

6pp0gazn

6pp0gazn1#

XPath看起来不错,我猜您需要执行一些explicit waitimplicit wait操作,以确保在分配元素之前页面已完全加载。
我想指出的另一点是,如果登录网址是可用的,那么定位登录按钮似乎是多余的。您可以通过driver.get('https://login.qgenda.com/')直接访问它
例如,

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
driver.get('https://login.qgenda.com/')
input_email = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="Input_Email"]'))
)
input_email.send_keys('Josh')

你可以阅读更多关于它here.

相关问题