Trying to select link text within a table cell if the desired text is also within the cell

冷暖自知 提交于 2020-01-05 13:07:59

问题


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]

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.

Here is what I've tried:


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")

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>


回答1:


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")


来源:https://stackoverflow.com/questions/57012678/trying-to-select-link-text-within-a-table-cell-if-the-desired-text-is-also-withi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!