我有一个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倍。
我希望我能解释我想要的东西,一切都很清楚。
感谢您的帮助:)
答案 0 :(得分:0)
XPATH可以为你做到:
all_links = driver.find_elements_by_xpath(
'//table//td[contains(@class, "status")]/span[not(contains(text(), "DONE"))]/../../td/a'
)
答案 1 :(得分: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()
答案 2 :(得分: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
我希望我的例子可以提供帮助。