以下 html 代码会生成一个弹出窗口以添加产品。试图找到可以在 selenium 中运行的 xpath 或 css,允许单击“添加”按钮添加新产品。
我尝试使用此 xpath//div[18]/div[2]/div/button,它仅适用于一个 session 。当您关闭浏览器并重新打开时,上面的 xpath 不起作用。那么,在这种情况下,我们如何编写一个不必传递 div[number] 的 xpath div[18]。
<div style="height: auto; min-height: 104px; width: auto;" class="dialog ui-dialog-content ui-widget-content">
<fieldset><legend>search</legend>
<div id="nameorids">
<input type="radio">name
<input type="radio">id
</fieldset>
<div style="clear: both; padding-top: 5px; float: right;"><button class="add">add</button></div></div>
最佳答案
首先,此 HTML 无效:nameorids
div 在字段集关闭之前未关闭。打字错误?
如果您可以影响 HTML,请尝试向外部 div 添加 id 或 class 属性,以便您可以使用 css 选择器定位它们。例如。如果你能让 HTML 看起来像
<div id="foo" ...
然后你可以使用选择器#foo button.add
。
关于css - 以下 html 片段需要 xpath 或 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7276964/