【问题标题】:Need a generic xpath for the following html code需要以下 html 代码的通用 xpath
【发布时间】:2016-12-08 23:03:09
【问题描述】:

以下是我需要唯一 XPath 的 HTML 代码。 Firebug 给了我一个类似

的 xpath
.//*[@id='service_dlg']/form/p[4]/span/input[1] [For Essential]

.//*[@id='service_dlg']/form/p[4]/span/input[2] [For Enhanced]

我需要像[@name = 'Essential'] 这样的东西,这样我就不需要在我的代码中编写多个xpath。我想从函数中传递像essential, enhanced and premium 这样的值。

<p>
<label style="vertical-align:top">Feature Pack:</label>
<span style="display:inline-block">
<input name="pack" value="Essential" type="radio">
<label class="feature">Essential</label>
<input name="pack" value="Enhanced" type="radio">
<label class="feature">Enhanced</label>
<input name="pack" value="Premium" type="radio">
<label class="feature">Premium</label>
<br>
<input name="featurepack" value="u" type="checkbox">
<label class="feature">URL Filtering</label>
<br>
<input name="featurepack" value="t" type="checkbox">
<label class="feature">Threat Prevention</label>
<br>
<input name="featurepack" value="w" type="checkbox">
<label class="feature">Wildfire</label>
<br>
</span>

【问题讨论】:

  • 我只会使用 css 选择器input[name=pack][value="Essential"],您可以在其中放置变量而不是 Essential。 CSS 选择器比 xpath 好很多
  • 它没有用。我在firebug上尝试了你建议的css选择器,它说没有找到匹配的节点。
  • 它是 IFRAME 吗?你能发布一个链接到页面吗?
  • 不,它不是 iframe。它是一个企业 GUI,在 Internet 上不可用。

标签: python selenium-webdriver


【解决方案1】:

尝试如下Enhanced:

//input[@value='Enhanced']

要使其通用并作为函数参数接收值,请尝试如下:

driver.find_element_by_xpath("//input[@value='%s']" % value) # where value is function parameter, can be Essential, Enhanced or Premium

【讨论】:

  • 在 SO 中表示感谢的最佳方式是接受并支持答案。由于您是新来者,请告知?...
  • 我的错。我接受了答案。再次非常感谢您。
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多