python - 如何编写Xpath进行动态值传递?

标签 python python-3.x selenium-webdriver

如何为以下内容编写 Xpath:

<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze
(&quot;1622662&quot;,&quot; SP0001622662&quot;,&quot;CS3_pro2_axeda6&quot;,&quot;5336293761&quot;);"> 

screenshot

某些变量(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(&quot;" + str(st_string) + "&quot;,&quot;SP0001622662&quot;,&quot;CS3_pro2_axeda6&quot;,&quot;5336293761&quot;);"
                #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(&quot;1622662&quot;,&quot;SP0001622662&quot;,&quot;CS3_pro2_axeda6&quot;,&quot;5336293761&quot;);">

最佳答案

如果您要查找的值是 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/

相关文章:

python - 在 python 2.6 中逐行迭代两个文件的首选方法

python - 属性错误: module 'cupy' has no attribute 'array'

java - 为 selenium webdriver 数据驱动框架读取 excel 中的空单元格

Python:Matplotlib imshow shift xlabel 数字

python - 带有通配符的 Scrapy 选择 id

python - 如何在 Tkinter Canvas 中的一条线上重绘

javascript - webdriverio 元素在箭头函数内未定义

python - bash 中的标准输出被一行代码阻塞

python - 大小为 x 的 1 的二维阵列菱形

python-3.x - Pandas 基于 groupby 创建百分位字段,级别为 1