python webdriver在带有文本的范围内按类查找元素

时间:2016-11-03 00:31:58

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

我使用Python 2.7,Webdriver和Chrome访问Pinterest以将图像插入到主板上。我已成功登录该站点并创建了一个板。我遇到的问题是当我的代码尝试单击add 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

     

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

我为addbutton.click()得到以下异常:----

  

引发exception_class(消息,屏幕,堆栈跟踪)

     

WebDriverException:消息:未知错误:元素无法点击   在点(871,845)。其他元素将收到点击:   (会话信息:chrome = 54.0.2840.71)(驱动信息:   chromedriver = 2.25.426923   (0390b88869384d6eb0d5d09729679f934aab9eed),platform = Windows NT   10.0.14393 x86_64)

1 个答案:

答案 0 :(得分:0)

您正在尝试访问由XPath正确找到的<span>标记,我不确定点击它会做什么。您是否尝试过获取<button>的XPath?

请尝试使用此代码:

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

这对我有用。