【问题标题】:Selenium Webdriver python XPath not workingSelenium Webdriver python XPath不起作用
【发布时间】:2018-09-30 09:01:05
【问题描述】:

我正在尝试使用 Python 和 selenium webdriver 自动化流程。 我能够成功登录并导航到我想要发布内容的页面,但由于某种原因,系统无法识别 xpath。

它给出了以下错误:

NoSuchElementException:无法定位元素://*[@id="widecol"]/div/form/p[1]/input

这是我的代码:

from selenium import webdriver
mydriver = webdriver.Firefox()
mydriver.get("https://www.phishtank.com/")
mydriver.maximize_window()
mydriver.find_element_by_xpath('//*[@id="username"]').send_keys("myusername")
mydriver.find_element_by_xpath('//*[@id="password"]').send_keys("mypassword")
mydriver.find_element_by_xpath('//*[@id="header"]/div[2]/form/input[3]').click()
mydriver.find_element_by_xpath('//*[@id="nav"]/ul/li[2]/a').click()
mydriver.find_element_by_xpath('//*[@id="widecol"]/div/form/p[1]/input').send_keys("sample text testing to fill form")

这是我的 HTML 代码

<div id="widecol">
<div class="padded">
    <form method="POST">
    <h2>Add A Phish</h2>
    <ol>
        <li>Visit our <b><a href="what_is_phishing.php">What is phishing?</a></b> page to confirm that the suspected phish meets all of the criteria.</li>
        <li>Add a phish using the form below, or even better, submit a phish directly <a href="mailto:phish@phishtank.com">via email</a>.</li>
    </ol>
    <h3>Phish URL:</h3>
            <p>
            <input type="text" name="phish_url" style="width:90%;" value="" /><br />
            <span class="small">Copy and paste the URL of the phishing website.</span>
    </p>
    <h3>What is the organization referenced in the email?</h3>
            <p class="slim">
        <select name="phish_target">

这给了我以下错误:

NoSuchElementException:无法定位元素://*[@id="widecol"]/div/form/p[1]/input

这是 HTML 代码:

<input type="text" name="phish_url" style="width:90%;" value=""> outer HTML code

这是我的 XPath:

//*[@id="widecol"]/div/form/p[1]/input - Xpath

请告诉我去哪里找,谢谢。

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    您需要诱导 WebDriverWait 元素可点击,您可以使用以下代码行:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='phish_url' and @type='text']"))).send_keys("sample text testing to fill form")
    

    【讨论】:

    • 您好,感谢您的帮助。我尝试破坏上面的代码行,但它仍然给出相同的错误: NoSuchElementException: Unable to locate element: //*[@id='widecol']//input[@name='phish_url']
    • @Kunal 我相信您还没有尝试过我的代码,因为我的解决方案不包含@id='widecol'。试一试我的代码,让我知道状态。
    • 完美,有效。非常感谢。抱歉,我忘了删除旧的 xpath 行,这对我很有帮助。
    【解决方案2】:

    没有完整的 HTML,我们无法判断 XPath 是否正确。 您能否尝试包含更多外部 Html 代码?

    您可以在单击最后一个 URL 后尝试等待,可能该元素尚未加载。 您还可以检查输入是否在 iframe 中,这可能会导致问题。

    【讨论】:

    【解决方案3】:

    您可以尝试以下方法吗?

    //*[@id='widecol']//input[@name='phish_url']
    

    应该适用于该输入。只是为了试错,请在执行此操作之前稍等片刻。

    【讨论】:

    • 嗨,谢谢。 Itried 运行它,但它给出了相同的错误: NoSuchElementException: Unable to locate element: //*[@id='widecol']//input[@name='phish_url']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2017-02-23
    • 2012-04-22
    相关资源
    最近更新 更多