Python - 不要单击if语句为true

问题描述 投票:1回答:3

我有一个x行和4列的表。我的代码点击了表格中的每个链接。

+----------+-------------+-----------------+--------------+
|    NR    |    Name     |     Assignee    |    Status    |
+----------+-------------+-----------------+--------------+
|    1     |     ABC     |        ME       |     DONE     |
|    2     |     DEF     |        ME       |    In Work   |
|    3     |     GHI     |       PAUL      |     DONE     |
+----------+-------------+-----------------+--------------+

*表格仅供更好的解释。

现在我希望我的代码在状态为完成时不会点击链接!

这是我的代码:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(url)

total_tickets = driver.find_elements_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr")
    # Click throug all Tickets
    for j in range(0,len(total_tickets)):
        ticket = driver.find_elements_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[2]/a")
        status = driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[5]/span").text
        if status == "DONE":
            for k in range(0,len(total_tickets)):
                ticket_name = driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[2]/a").text
                print ticket_name,"is allready done !"
        else:
            ticket[j].click()
            print driver.title

以下是本网站HTML表格的一部分:

<table id="ghx-issues-in-epic-table" class="ghx-issuetable " data-rendered="1513339726797">
    <tbody>
        <tr data-issuekey="1" class="issuerow">
            <td class="nav ghx-minimal ghx-alt"></td>
            <td class="nav ghx-minimal">
                <a href="/browse/1">1</a>
            </td>
            <td class="nav ghx-summary">
                ABC
            </td>
            <td class="assignee">
                ME
            </td>
            <td class="nav status">
                <span class="status">
                    DONE
                </span>
            </td>
        </tr>    
    </tbody>
</table>

我没有收到错误。输出是第一个链接为DONE然后停止的4倍。

我希望我能解释我想要的东西,一切都很清楚。

谢谢你的帮助 :)

python selenium click
3个回答
0
投票

XPATH可以为你做到:

all_links = driver.find_elements_by_xpath(
    '//table//td[contains(@class, "status")]/span[not(contains(text(), "DONE"))]/../../td/a'
)

0
投票

要使用Status != DONE单击票证,您可以使用以下代码块:

all_status = driver.find_elements_by_xpath("//table[@id='ghx-issues-in-epic-table']/tbody/tr//td[@class='nav status']/span[@class='status']")
for status in all_status :
    if ("DONE" not in status.get_attribute("innerHTML")) :
    status.find_element_by_xpath("//self/preceding::td[@class='nav ghx-minimal']/a").click()

0
投票

嗨@diem_L其他人做了一些例子,但我会给出一个不同的建议......元素在一个表中,你的例子中的xpath是:

status = driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[5]/span").text

如果另一列具有相似的xpath,但可能只更改行,它们是:

driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[5]/span").text
driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[6]/span").text
driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[7]/span").text

如果你看到改变只是数字(验证它);如果是这种情况,您可以创建变量并执行循环。例:

l=len(LINE) #how many line you have in your table
i=0
while i<l:
    driver.find_element_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td["+str(i)+"]/span").text #in this case if you see instead than the number 5 I put the variable str(i) that increment in each line, if your table start from number 5 and not 0 instead that i you could have put str(5+i)
    i+=1

我希望我的例子能有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.