【问题标题】:invalid xpath expression python无效的xpath表达式python
【发布时间】:2018-05-03 23:46:49
【问题描述】:

我正在尝试获取所有以某些东西开头的 xpath,但是当我测试它时,它说我的 xpath 无效。

这是我的 xpath:

xpath = "//*[contains(@id,'ClubDetails_"+ str(team) +"']"

当我打印时输出:

//*[contains(@id,'ClubDetails_1']

这里是 HTML:

<div id="ClubDetails_1_386" class="fadeout">
   <div class="MapListDetail">
      <div>Bev Garman</div>
      <a href="javascript:DoMailTo('armadaswim@aol.com')">armadaswim@aol.com</a>
   </div>
   <div class="MapListDetail">
      <div>Rolling Hills Country Day School</div>
      <div>26444 Crenshaw Blvd</div>
      <div>Rolling Hills Estates, CA 90274</div>
   </div>
   <div class="MapListDetailOtherLocs_1">
      <div>This club also swims at other locations</div>
      <a href="javascript:ShowOtherLocs(386,'La Mirada Armada')"><span class="show_them_link">show them...</span></a>
   </div>
</div>

我错过了什么?

【问题讨论】:

  • 您还没有关闭contains 调用中的) 括号:xpath = "//*[contains(@id,'ClubDetails_"+ str(team) +"')]"

标签: python selenium xpath automated-tests


【解决方案1】:

另一种方法是使用模式匹配以某些文本开头的 ID,并获取列表中的所有这些元素,然后逐个迭代列表并检查该元素的 id 属性是否包含您想要的团队的条件。

您可以在 CSS 选择器中匹配模式,如下所示:

div[id^='ClubDetails_']

这就是在 xpath 中的方式:

//div[starts-with(@id, 'ClubDetails_')]

代码示例:

expectedid = "1_386"
clubdetails = driver.find_elements_by_css_selector("div[id^='ClubDetails_']")
for item in clubdetails:
    elementid = item.get_attribute('id')
    expectedid in elementid
    // further code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多