Selenium查找onclick的其他方法

huangapple go评论167阅读模式
英文:

Selenium Other ways to find_element for onclick

问题

我正在查看一个表格,其中有以下代码部分:
<tr onclick="setValue('115751')" role="row" class="odd">
我尝试使用Selenium来查找元素并根据setValue单击不同的行。由于我通常使用xpath,所以我尝试使用它,但xpath不起作用,因为有时顺序会更改。

  1. my_item = ''//*[@id="resulttable"]/tbody/tr[1]''
  2. mychoice = driver.find_element(By.XPATH, my_item)
  3. 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.

  1. my_item = '//*[@id="resulttable"]/tbody/tr[1]'
  2. mychoice = driver.find_element(By.XPATH, my_item)
  3. 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 -->

  1. table, th, td {
  2. border: 1px solid;
  3. }

<!-- language: lang-html -->

  1. &lt;table width=&quot;100%&quot; class=&quot;display dataTable no-footer&quot; id=&quot;resulttable&quot; cellspacing=&quot;0&quot; role=&quot;grid&quot; aria-describedby=&quot;resulttable_info&quot; style=&quot;width: 100%;&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr role=&quot;row&quot;&gt;
  4. &lt;th&gt;Statement&lt;/th&gt;
  5. &lt;th&gt;Recordation No&lt;/th&gt;
  6. &lt;th&gt;Status&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/thead&gt;
  9. &lt;tbody&gt;
  10. &lt;tr onclick=&quot;setValue(&#39;115751&#39;)&quot; role=&quot;row&quot; class=&quot;odd&quot;&gt;
  11. &lt;td&gt;2022-3301051&lt;/td&gt;
  12. &lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
  13. 3301051&lt;/td&gt;
  14. &lt;td&gt;Submitted&lt;/td&gt;
  15. &lt;/tr&gt;
  16. &lt;tr onclick=&quot;setValue(&#39;115529&#39;)&quot; role=&quot;row&quot; class=&quot;even&quot;&gt;
  17. &lt;td&gt;2022-3301053&lt;/td&gt;
  18. &lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
  19. 3301053&lt;/td&gt;
  20. &lt;td&gt;Submitted&lt;/td&gt;
  21. &lt;/tr&gt;
  22. &lt;tr onclick=&quot;setValue(&#39;115201&#39;)&quot; role=&quot;row&quot; class=&quot;odd&quot;&gt;
  23. &lt;td&gt;2022-3301309&lt;/td&gt;
  24. &lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
  25. 3301309&lt;/td&gt;
  26. &lt;td&gt;Not Submitted&lt;/td&gt;
  27. &lt;/tr&gt;
  28. &lt;tr onclick=&quot;setValue(&#39;115893&#39;)&quot; role=&quot;row&quot; class=&quot;even&quot;&gt;
  29. &lt;td&gt;2022-3301310&lt;/td&gt;
  30. &lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
  31. 3301310&lt;/td&gt;
  32. &lt;td&gt;Not Submitted&lt;/td&gt;
  33. &lt;/tr&gt;
  34. &lt;tr onclick=&quot;setValue(&#39;115497&#39;)&quot; role=&quot;row&quot; class=&quot;odd&quot;&gt;
  35. &lt;td&gt;2022-3301313&lt;/td&gt;
  36. &lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
  37. 3301313&lt;/td&gt;
  38. &lt;td&gt;Not Submitted&lt;/td&gt;
  39. &lt;/tr&gt;
  40. &lt;/tbody&gt;
  41. &lt;/table&gt;

<!-- end snippet -->

答案1

得分: 1

当根据tr标记的onclick属性进行指定时,您需要转义引号,使XPath看起来像这样:

  1. my_item = &quot;//tr[@onclick=\&quot;setValue(&#39;115497&#39;)\&quot;]&quot;

或者

  1. my_item = &quot;//tr[@onclick=&#39;setValue(\&#39;115497\&#39;)&#39;]&quot;

如果您希望将变量直接放入字符串文字中:

  1. value = 115497
  2. my_item = f&quot;//tr[@onclick=\&quot;setValue(&#39;{value}&#39;)\&quot;]&quot;
  3. // 或者 my_item = f&quot;//tr[@onclick=&#39;setValue(\&#39;{value}\&#39;)&#39;]&quot;
英文:

When specifying based on the onclick of the tr tag, you will have to escape the quotes so the xpath would look like this

  1. my_item = &quot;//tr[@onclick=\&quot;setValue(&#39;115497&#39;)\&quot;]&quot;

or

  1. my_item = &quot;//tr[@onclick=&#39;setValue(\&#39;115497\&#39;)&#39;]&quot;

in case you want to put the variables directly into the string literal:

  1. value = 115497
  2. my_item = f&quot;//tr[@onclick=\&quot;setValue(&#39;{value}&#39;)\&quot;]&quot;
  3. // or my_item = f&quot;//tr[@onclick=&#39;setValue(\&#39;{value}\&#39;)&#39;]&quot;

huangapple
  • 本文由 发表于 2023年4月7日 04:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定