python+selenium-- 定位table表格

时间:2024-03-13 22:17:48

定位table表格

 

 Table 表格固定格式:./*[@id=\'表格id\']/tbody/tr[行数]/td[列数]/a

from selenium import webdriver
from time import sleep


driver = webdriver.Firefox()
driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")
driver.implicitly_wait(10)

#登录
driver.find_element_by_xpath(".//*[@id=\'account\']").send_keys("admin")
driver.find_element_by_xpath(".//*[@name=\'password\']").send_keys("123456")
sleep(2)
driver.find_element_by_xpath(".//*[@id=\'submit\']").click()
sleep(5)
driver.get("http://127.0.0.1/zentao/bug-browse-1.html")
sleep(3)
table = ".//*[@id=\'bugList\']/tbody/tr[3]/td[4]/a"
driver.find_element_by_xpath(table).click()

sleep(3)
driver.close()

 

根据表格名称定位后边按钮

 

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")
driver.implicitly_wait(10)

# 登录
driver.find_element_by_xpath(".//*[@id=\'account\']").send_keys("admin")
driver.find_element_by_xpath(".//*[@name=\'password\']").send_keys("123456")
sleep(2)
driver.find_element_by_xpath(".//*[@id=\'submit\']").click()
sleep(5)

# 先定位bug标题,在定位父节点之后再定位编辑按钮

driver.get("http://127.0.0.1/zentao/bug-browse-1.html")
sleep(3)

title = "TestBug02"
#先通过bug标题定位,..为父级  再定位父级的父级,再定位td标签下a标签的编辑按钮
t = ".//*[text()=\'%s\']/../../td[@class=\'text-right\']/a[@title=\'编辑\']" % title

driver.find_element_by_xpath(t).click()
sleep(3)

driver.close()