【问题标题】:python webdriver find element by xpath by class within a span with textpython webdriver通过xpath在文本范围内按类查找元素
【发布时间】:2016-11-03 09:42:02
【问题描述】:

我使用 Python 2.7、Webdriver 和 Chrome 访问 Pinterest 以将图像插入到板。我已成功登录该站点并创建了一个板。我遇到的问题是,当我的代码尝试单击添加 pin 元素时,出现异常。

元素是页面右下角的“+”按钮(见附件“红色箭头”)。我在元素上附加了 Chrome 检查的图像。

我在研究中看到了一个答案,它表明带有 Chrome 的 Webdriver 在某些情况下存在“跨度”问题。请注意,我可以在按钮的任意位置手动单击该元素。

我的代码:----

print "e20 addbutton"

addbutton = driver.find_element_by_xpath("//span[@class='accessibilityText' and contains(text(), 'Save Pin')] ")

print addbutton
if addbutton.is_enabled():
    print "addbutton enabled"
    if addbutton.is_displayed():
        print "addbutton displayed"
        addbutton.click()

上面的输出是:----

e20 添加按钮

添加按钮已启用 添加按钮显示

addbutton.click() 出现以下异常:----

raise exception_class(message, screen, stacktrace)

WebDriverException:消息:未知错误:元素不可点击 在点 (871, 845)。其他元素会收到点击: (会话信息:chrome=54.0.2840.71)(驱动程序信息: 铬驱动程序=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),平台=Windows NT 10.0.14393 x86_64)

【问题讨论】:

  • 我看不到附件,单独添加。

标签: python-2.7 google-chrome selenium-webdriver


【解决方案1】:

您正在尝试访问您已通过 XPath 正确定位的 <span> 标记,我不确定单击它会做任何事情。您是否尝试过获取<button> 的XPath?

试试这个代码:

addbutton = driver.find_element_by_xpath('//button[@data-element-type="18"]')
# --- #
if addbutton.is_displayed():
    print "addbutton displayed"
    addbutton.click()

这对我有用。

【讨论】:

  • //button[@data-element-type="18" 工作,谢谢。我在任何问答论坛上都找不到这样的例子。
猜你喜欢
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2020-07-20
  • 2020-02-06
  • 2020-05-07
  • 2020-08-08
  • 2016-04-26
  • 2014-08-19
相关资源
最近更新 更多