Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Xpath:根据相邻元素的属性匹配一个元素_Python_Selenium_Xpath_Selenium Chromedriver - Fatal编程技术网

Python Xpath:根据相邻元素的属性匹配一个元素

Python Xpath:根据相邻元素的属性匹配一个元素,python,selenium,xpath,selenium-chromedriver,Python,Selenium,Xpath,Selenium Chromedriver,我尝试使用Selenium/Python使用以下格式的标记: <tr> <td><a href="www.google.com">google</a></td> <td>useless text</td> <td>useless text2</td> <td>useless text3</td>

我尝试使用Selenium/Python使用以下格式的标记:

    <tr>
        <td><a href="www.google.com">google</a></td>
        <td>useless text</td>
        <td>useless text2</td>
        <td>useless text3</td>
        <td><a href="needle@email.com">emailaddress</a></td>
    </tr>
但我得到了一个错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"xpathhere"}
我确实知道使用xpath访问“needle@email.com“a是正确的,因为它只是从chrome开发工具中复制的,所以在到达第一个
a
元素后,xpath部分肯定会出错。有人能解释一下我的xpath的问题吗?

首先,请注意(这可能是一个毫无意义的输入错误),您正在寻找“mailto:needle@email.com“而href属性的值为”needle@email.com“

第二,你实际上知道如何回去[…]。但是因此,为什么这也是一个毫无意义的打字错误

无论如何,这个xpath会让你的兄弟姐妹

'//a[contains(@href, "needle@email.com")]/../../td[1]/a[1]'
或者比使用
包含
更准确(因为您可能有其他可以匹配的电子邮件地址,例如。“otherneedle@email.com))

或者更好,即没有索引,没有父/子类的探索

'//td[a[@href="needle@email.com"]]/preceding-sibling::td/a'
所有测试。

首先,请注意(这可能是一个无意义的打字错误),您正在寻找“mailto:needle@email.com“而href属性的值为”needle@email.com“

第二,你实际上知道如何回去[…]。但是因此,为什么这也是一个毫无意义的打字错误

无论如何,这个xpath会让你的兄弟姐妹

'//a[contains(@href, "needle@email.com")]/../../td[1]/a[1]'
或者比使用
包含
更准确(因为您可能有其他可以匹配的电子邮件地址,例如。“otherneedle@email.com))

或者更好,即没有索引,没有父/子类的探索

'//td[a[@href="needle@email.com"]]/preceding-sibling::td/a'

全部测试。

尝试查找包含该电子邮件的
tr
,然后单击其中的第一个链接

//tr[.//a[contains(@href, 'your_email')]]//a


尝试查找包含该电子邮件的
tr
,然后单击其中的第一个链接

//tr[.//a[contains(@href, 'your_email')]]//a


你的HTML应该是这样的

<tr>
    <td><a href="www.google.com">google</a></td>
    <td>useless text</td>
    <td>useless text2</td>
    <td>useless text3</td>
    <td><a href="mailto:needle@email.com">emailaddress</a></td>
</tr>
我使用了
contains
,因为链接中的电子邮件地址可能类似于
mailto:Jose Greco


顺便说一句,我刚刚在我的机器上执行了这些东西。

你的HTML应该是这样的

<tr>
    <td><a href="www.google.com">google</a></td>
    <td>useless text</td>
    <td>useless text2</td>
    <td>useless text3</td>
    <td><a href="mailto:needle@email.com">emailaddress</a></td>
</tr>
我使用了
contains
,因为链接中的电子邮件地址可能类似于
mailto:Jose Greco

顺便说一句,我刚刚在我的机器上执行了这些东西。

尝试使用以下代码:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

xpath = '//td[a[@href="needle@email.com"]]/preceding-sibling::td/a'
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
这将允许您根据表行中最后一个链接(
tr
)的
href
属性匹配第一个链接,并在其可单击时单击它尝试使用以下代码:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

xpath = '//td[a[@href="needle@email.com"]]/preceding-sibling::td/a'
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()

这应该允许您根据表行中最后一个链接(
tr
)的
href
属性匹配第一个链接,并在其可单击时单击它

可能只是
//a[contains(@href)needle@email.com”)
?问题是我试图检索第一个
td
中的
a
,所以我需要弄清楚,一旦needle@email.com“
a
是匹配的。你应该避免使用索引,总是使用你知道不会改变的东西,比如属性的一部分。例如,使用索引是一种不好的做法,也不是很灵活。最好的答案是@Andersson's。”。没有索引,对意外问题和最先进的实践都很健壮。可能只是
//a[contains(@href,”needle@email.com)
?问题是我正在尝试检索第一个
td
中的
a
,因此我需要找出在needle@email.com"
a
是匹配的。你应该避免使用索引,总是使用一些你知道不会改变的东西,比如属性的一部分。例如,使用索引是一种不好的做法,不是很灵活。最好的答案是@Andersson。无索引,对意外问题和硒最新实践具有鲁棒性。无索引,对意外问题和硒最新实践具有鲁棒性。无索引,对意外问题和硒最新实践具有鲁棒性。