【问题标题】:How do i find the correct xpath for python selenium program, it keeps saying unable to locate?我如何为 python selenium 程序找到正确的 xpath,它一直说无法定位?
【发布时间】:2022-01-09 15:59:02
【问题描述】:

我试图从 twitter 登录页面获取用户名框,我遵循了很多在线教程,但它们都使用 twitter 的旧登录页面,而且我不太擅长编码以便能够理解如何。我想找到用户名框并向其发送用户名。

import csv
from getpass import getpass
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from msedge.selenium_tools import Edge, EdgeOptions

options = EdgeOptions()
options.use_chromium = True
driver = Edge(options=options)
driver.get('https://www.twitter.com/login')
driver.maximize_window()


username=driver.find_element_by_xpath('//input[@name="username"]').send_keys('Username')
time.sleep(2)

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    twitter上用户名字段的HTML代码是:

    <input autocapitalize="none" autocomplete="username" autocorrect="off" inputmode="text" name="username" spellcheck="false" type="text" dir="auto" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu" data-testid="ocfEnterTextTextInput" value="">
    

    观察name 属性值为username

    所以,尝试使用这个 xpath //*[@name="username"]

    【讨论】:

    • 那没用,谢谢你的尝试。
    • @Che 我在发布之前检查了代码。你能发布你更新的代码吗?
    • 对不起,我猜我编程不太好,我用你的答案编辑了帖子,它说它仍然找不到元素
    • @Che 您在哪个浏览器上执行脚本?
    • 到目前为止我已经尝试过 chrome 和 edge
    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2019-11-15
    • 1970-01-01
    • 2016-09-17
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多