当前位置: 首页 > 知识库问答 >
问题:

Selenium Webdriver python XPath不工作

阎冠玉
2023-03-14

我正在尝试使用Python和SeleniumWebDriver自动化一个过程。我能够成功登录并导航到我想要发布内容的页面,但由于某些原因,系统无法识别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

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

共有3个答案

缑文栋
2023-03-14

你能试试下面的吗?

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

应该对这一输入有用。为了试验/错误起见,请在执行此操作之前尝试一次小的等待。

谷梁振
2023-03-14

如果没有完整的HTML,我们无法判断XPath是否正确。请尝试包含更多外部HTML代码,好吗?

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

濮阳立果
2023-03-14

您需要诱导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")
 类似资料:
  • 我想在菜单栏文本被选中时更改它的颜色。 这里可能出了什么问题? 我尝试使用伪类':active',但没有得到应用。其中as':Hover'正在工作。 我还尝试使用'Router LinkActive',它应该添加类'Active-Link',但这也不起作用。 我在下面给出了HTML、SCCS和TS代码:

  • 我编写了一组简单的类,向一位朋友演示如何为AOP(而不是xml配置)使用注释。我们无法使@ComponentScan工作,并且AnnotationConfigApplicationContext getBean的行为也不正常。我想明白两件事。请参阅下面的代码: PersonOperationSI.java PersonOperations.java PersonOperationsConfigCl

  • 我正在Eclipse Neon中使用Hibernate工具(JBoss tools 4.4.0.Final)。现在,我想将数据库表反向工程为POJO对象和Hibernate映射文件。 我遵循了一些关于如何设置Eclipse来生成POJO对象的教程。在我运行配置之前,一切看起来都很好。什么都没发生,也没有抛出错误。有人能帮我吗?数据库是一个微软SQL服务器2014。 我的逆向工程配置文件看起来像:

  • 我正在尝试使用codeigniter insert\u batch将多行插入到我的数据库表中。根据错误报告,似乎没有设置表列。只是阵列的数量: 我的看法是: 我的控制器: 和型号:

  • 我尝试使用StreamWriter.WriteLine(不是静态地)将几行代码一次写到。txt文件中。 每个播放器对象都是字符串cosnatants。如果我使用不同的文件名(也称为BasicTestInfo2.txt),它会在bin.debug中创建该文件,但它是空的。我知道我到达了using块的内部(我在里面放了一个console.writeline),我知道我想要截断,这就是为什么我对appe

  • 我正在尝试使用yii2邮件组件发送电子邮件。 配置web。php 还有我的代码。 我收到了这个错误。 Swift\u TransportException预期响应代码为250,但收到代码“535”,消息“535-5.7.8用户名和密码不被接受。有关详细信息,请访问535 5.7.8https://support.google.com/mail/?p=BadCredentialsa13-v6sm41

  • 问题内容: 似乎不起作用,但确实起作用。有什么想法吗? 问题答案: 您不能在Java中将基本类型用作通用参数。改为使用: 使用自动装箱/拆箱,代码几乎没有区别。自动装箱意味着您可以编写: 代替: 自动装箱意味着将第一个版本隐式转换为第二个版本。自动拆箱意味着您可以编写: 代替: 如果未找到键,则隐式调用意味着将生成一个,例如: 原因是类型擦除。例如,与C#不同,泛型类型不会在运行时保留。它们只是显