如何为以下内容编写 Xpath:
<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze
("1622662"," SP0001622662","CS3_pro2_axeda6","5336293761");">
某些变量(st_name)中有突出显示的值。高亮显示和红色舍入值将动态变化。 我不知道如何为此编写 Xpath。
import xlrd
path = r'C:\Users\tmou\PycharmProjects\Python\WebScraping\Book2.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
try:
if st == float(st):
st_string = int(st)
#variable = 1622662
#new_string = "javascript:popAnalyze("" + str(st_string) + "","SP0001622662","CS3_pro2_axeda6","5336293761");"
#driver.find_element_by_xpath("//input[@class='t2']/@onclick='" + st_string + "'").click()
#driver.find_element_by_xpath("//input[@value='Analyze' and contains(@onclick='" + st_string + "']")
#driver.find_element_by_xpath("//a[@title='" + st_string + "']")
HTML:
<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze("1622662","SP0001622662","CS3_pro2_axeda6","5336293761");">
最佳答案
如果您要查找的值是 onclick 属性下的值,则以下 Xpath 表达式应该有效:
string(//input[@class='t2']/@onclick)
编辑 1
您可以尝试使用 XPath 版本 3 或更低版本吗:
//input[(@class='t2' and matches(@onclick,'1622662'))]
对于 XPath 3.1 版:
//input[@class='t2']/[matches(@onclick, '1622662')]
关于python - 如何编写Xpath进行动态值传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55001546/