如果想要的文本也在单元格中,则尝试选择表格单元格中的链接文本 [英] Trying to select link text within a table cell if the desired text is also within the cell

查看:101
本文介绍了如果想要的文本也在单元格中,则尝试选择表格单元格中的链接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个同时包含文本和链接的表格单元格.现在,该表中的每一行都有相同的文本链接超链接标题"[Details]",但是位置会根据您所在的行而改变.

So I have a table cell that contains both text and a link. Now every row in this table has the same text link hyperlink title "[Details]" but the location changes on where it takes you based on which row you are in.

例如,该单元格将如下所示:我想要的文本" [详细信息]

For example the cell will look like this: "Text I Want" [Details]

我希望能够根据该单元格中还包含哪些文本来转到正确的链接,但是在弄清楚如何在Python中进行编码时遇到了一些问题.单击该表中的一行后,它将在表中移动它的位置,因此不再使用Xpath.

I want to be able to go to the correct link based on what text is also within that cell, but am having some issues figuring out how to code that in Python. Once a row in this table is clicked it moves it location in the table so using Xpath is out.

这是我尝试过的:


MyText = driver.find_element_by_xpath("//span[text()='My Desired Text']")
MyText.find_element_by_partial_link_text("Details").click()



def click_me(myString):
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//td/span[.='" + myString + "']//following::span[2]"))).click()

click_me("My Desired Text")

但是我认为我需要使用if语句才能真正获得所需的结果.任何建议,将不胜感激.

But I think I'll need to use if statements to actually get the desired results. Any suggestions would be appreciated.


<td>
                            <span>My Desired Text</span>
                            <span class="HSpacer10"></span>
                            <span class="commonLink" onclick="handleMyEvents(EVENT_EDIT_PKG, 60000,1);">[Details]</span>


                        </td>

推荐答案

由于该表由多个文本以及与 Details 具有相同文本的关联链接组成,因此您可以编写以下方法:

As the table is consisted of multiple texts and associated links with same text as Details you can write a method as follows:

def click_me(myString):
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td/span[.='" + myString + "']//following::span[2]"))).click()

现在,您可以使用任何所需的文本来调用此方法,以单击关联的链接:

Now you can call this method with any of the desired texts to click on the associated link:

click_me("My Desired Text")
# or
click_me("My Text")
# or
click_me("Text")

这篇关于如果想要的文本也在单元格中,则尝试选择表格单元格中的链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆