Selenium查找onclick的其他方法

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

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 -->

&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;
&lt;thead&gt;
&lt;tr role=&quot;row&quot;&gt;
&lt;th&gt;Statement&lt;/th&gt;
&lt;th&gt;Recordation No&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr onclick=&quot;setValue(&#39;115751&#39;)&quot; role=&quot;row&quot; class=&quot;odd&quot;&gt;
&lt;td&gt;2022-3301051&lt;/td&gt;
&lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
3301051&lt;/td&gt;
&lt;td&gt;Submitted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr onclick=&quot;setValue(&#39;115529&#39;)&quot; role=&quot;row&quot; class=&quot;even&quot;&gt;
&lt;td&gt;2022-3301053&lt;/td&gt;
&lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
3301053&lt;/td&gt;
&lt;td&gt;Submitted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr onclick=&quot;setValue(&#39;115201&#39;)&quot; role=&quot;row&quot; class=&quot;odd&quot;&gt;
&lt;td&gt;2022-3301309&lt;/td&gt;
&lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
3301309&lt;/td&gt;
&lt;td&gt;Not Submitted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr onclick=&quot;setValue(&#39;115893&#39;)&quot; role=&quot;row&quot; class=&quot;even&quot;&gt;
&lt;td&gt;2022-3301310&lt;/td&gt;
&lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
3301310&lt;/td&gt;
&lt;td&gt;Not Submitted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr onclick=&quot;setValue(&#39;115497&#39;)&quot; role=&quot;row&quot; class=&quot;odd&quot;&gt;
&lt;td&gt;2022-3301313&lt;/td&gt;
&lt;td class=&quot;hidden-xs hidden-sm sorting_1&quot;&gt;
3301313&lt;/td&gt;
&lt;td&gt;Not Submitted&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案1

得分: 1

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

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

或者

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

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

value = 115497
my_item = f&quot;//tr[@onclick=\&quot;setValue(&#39;{value}&#39;)\&quot;]&quot;
// 或者 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

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

or

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:

value = 115497
my_item = f&quot;//tr[@onclick=\&quot;setValue(&#39;{value}&#39;)\&quot;]&quot;
// 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:

确定