Selenium send_keys - 方法发送xpath(Python异常行为)

发布于 2025-01-10 12:27:51 字数 1290 浏览 5 评论 0原文

我正在开发一个 Tinder 项目,为此我想使用 google 登录。我设法隐藏并且可以成功登录,但是当我使用 send_keys 方法发送电子邮件时,它会随电子邮件一起发送电子邮件输入元素的 xpath(它还会发送位于我的代码结束)。这是一种奇怪的行为,你知道如何解决吗?

代码:

if __name__ == '__main__':
driver = uc.Chrome()
driver.get('https://tinder.com/')

wait()
current_driver = driver.current_window_handle

wait()
login = driver.find_element(by=By.CLASS_NAME, value='button')
login.click()
wait()
wait()

accept_cookeis = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/div[2]/div/div/div[1]/button')
wait()
accept_cookeis.click()
wait()
main_page = driver.current_window_handle
wait()
google_login = driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[1]/div/div[3]/span/div[1]/div/button/span[2]')
wait()
google_login.click()
wait()
my_windows = driver.window_handles
wait()

for window in my_windows:
    if window != main_page:
        driver.switch_to.window(window)
        wait()
        email_input = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input')
        wait()
        email_input.send_keys(YOUR_EMAIL)
        time.sleep(1000)

这是该方法发送的内容

I am working on a Tinder project and for that I would like to log in with google. I managed to hide from and I could log in with success, however when I use the send_keys method to send my email, it sends the xpath of the email input element along side with the email( also it sends the wait method which is at the end of my code). it is a weird behavior, do you know how to solve it?

CODE:

if __name__ == '__main__':
driver = uc.Chrome()
driver.get('https://tinder.com/')

wait()
current_driver = driver.current_window_handle

wait()
login = driver.find_element(by=By.CLASS_NAME, value='button')
login.click()
wait()
wait()

accept_cookeis = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/div[2]/div/div/div[1]/button')
wait()
accept_cookeis.click()
wait()
main_page = driver.current_window_handle
wait()
google_login = driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[1]/div/div[3]/span/div[1]/div/button/span[2]')
wait()
google_login.click()
wait()
my_windows = driver.window_handles
wait()

for window in my_windows:
    if window != main_page:
        driver.switch_to.window(window)
        wait()
        email_input = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input')
        wait()
        email_input.send_keys(YOUR_EMAIL)
        time.sleep(1000)

Here's what the method sends

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2025-01-17 12:27:51

所以我使用了一个 JavaScript 片段,它解决了问题,但我仍然不知道是什么原因导致的。

例子:

driver.execute_script('return document.getElementsByName("username")[0].value')

So I used a JavaScript snippet and it solved the problem, however I still don't know what causes this.

Example:

driver.execute_script('return document.getElementsByName("username")[0].value')

安稳善良 2025-01-17 12:27:51

添加

import time

在代码的开头。
这样,

time.sleep(1000)

就可以按预期工作。

Add

import time

at beginning of your code.
so that,

time.sleep(1000)

works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文