英文:
Selenium Other ways to find_element for onclick
问题
我正在查看一个表格,其中有以下代码部分:
<tr onclick="setValue('115751')" role="row" class="odd">
我尝试使用Selenium来查找元素并根据setValue单击不同的行。由于我通常使用xpath,所以我尝试使用它,但xpath不起作用,因为有时顺序会更改。
my_item = ''//*[@id="resulttable"]/tbody/tr[1]''
mychoice = driver.find_element(By.XPATH, my_item)
mychoice.click()
如何让Selenium根据setValue或表格中显示的“Recordation No”单击my_item?我在下面提供了来自网站的一些演示内容。
英文:
I am looking at a table that has this <tr onclick="setValue('115751')" role="row" class="odd">
like shown below. I am trying use Selenium to find_element and click on distinct rows based on the setValue. Since I normally use xpath I tried to use that but xpath does not work because some times the order changes.
my_item = '//*[@id="resulttable"]/tbody/tr[1]'
mychoice = driver.find_element(By.XPATH, my_item)
mychoice.click()
How do I get the selenium to click my_item based on the setValue or on the "Recordation No" shown in the table?
I have a little demo content from the website below.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
table, th, td {
border: 1px solid;
}
<!-- language: lang-html -->
<table width="100%" class="display dataTable no-footer" id="resulttable" cellspacing="0" role="grid" aria-describedby="resulttable_info" style="width: 100%;">
<thead>
<tr role="row">
<th>Statement</th>
<th>Recordation No</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr onclick="setValue('115751')" role="row" class="odd">
<td>2022-3301051</td>
<td class="hidden-xs hidden-sm sorting_1">
3301051</td>
<td>Submitted</td>
</tr>
<tr onclick="setValue('115529')" role="row" class="even">
<td>2022-3301053</td>
<td class="hidden-xs hidden-sm sorting_1">
3301053</td>
<td>Submitted</td>
</tr>
<tr onclick="setValue('115201')" role="row" class="odd">
<td>2022-3301309</td>
<td class="hidden-xs hidden-sm sorting_1">
3301309</td>
<td>Not Submitted</td>
</tr>
<tr onclick="setValue('115893')" role="row" class="even">
<td>2022-3301310</td>
<td class="hidden-xs hidden-sm sorting_1">
3301310</td>
<td>Not Submitted</td>
</tr>
<tr onclick="setValue('115497')" role="row" class="odd">
<td>2022-3301313</td>
<td class="hidden-xs hidden-sm sorting_1">
3301313</td>
<td>Not Submitted</td>
</tr>
</tbody>
</table>
<!-- end snippet -->
答案1
得分: 1
当根据tr
标记的onclick
属性进行指定时,您需要转义引号,使XPath看起来像这样:
my_item = "//tr[@onclick=\"setValue('115497')\"]"
或者
my_item = "//tr[@onclick='setValue(\'115497\')']"
如果您希望将变量直接放入字符串文字中:
value = 115497
my_item = f"//tr[@onclick=\"setValue('{value}')\"]"
// 或者 my_item = f"//tr[@onclick='setValue(\'{value}\')']"
英文:
When specifying based on the onclick
of the tr tag, you will have to escape the quotes so the xpath would look like this
my_item = "//tr[@onclick=\"setValue('115497')\"]"
or
my_item = "//tr[@onclick='setValue(\'115497\')']"
in case you want to put the variables directly into the string literal:
value = 115497
my_item = f"//tr[@onclick=\"setValue('{value}')\"]"
// or my_item = f"//tr[@onclick='setValue(\'{value}\')']"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论