css - 以下 html 片段需要 xpath 或 css

标签 css xpath selenium

以下 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/

相关文章:

xml - 从 XML 返回元素的第一个属性

xpath - 如何编写下面的正则表达式以在 Webdriver 中使用?

python - Xpath 中的空文本

html - 如何在 CSS 中制作没有图像的自定义选择选项菜单样式?

html - 按图标展开更多文本

html - LoC 方面的 CSS 优化

java - 如何处理 Selenium 中的多个警报弹出确认?

javascript - 如何通过名称-javascript/jquery 将焦点设置在下拉菜单上

ios - Appium iOS Error Domain=NSCocoaErrorDomain Code=260 "The file “WebDriverAgentRunner-Runner.app” 无法打开因为没有那个文件

python - 使用 selenium webdriver 登录网站时出错