无法使用Python 3.5.1&选择并单击网站按钮硒2.53.2

时间:2016-05-22 11:31:53

标签: javascript python selenium web-scraping

我目前正在使用运行OS X El Capitan的Macbook Air。我正在使用Python IDLE并尝试选择并单击以下网页按钮:

<div class="search-form-actions">
  <button class="btn btn-submit js-search-form-submit" selector=".js-search-form-submit" helptip="When you are ready to view all the contacts that match your criteria, click View Results." data-placement="top" data-step-number="5" data-step-name="Get Results">View Results</button>
</div>

使用以下脚本:

from selenium import webdriver
browser = webdriver.Firefox()
type(browser)
browser.get('https://app.avention.com/')

#enter username
usernameElem = browser.find_element_by_name("username")
usernameElem.send_keys('myusername')
usernameElem.submit()

#enter password
usernameElem = browser.find_element_by_name("password")
usernameElem.send_keys('mypassword')
usernameElem.submit()

#selecting 'Saved Searches' page
htmlElem = browser.find_element_by_tag_name("html") #without this line, site blocks next command
browser.get('https://app.avention.com/search/saved/7a1fbf2d-329a-4fda-8c51-4be5bc07317c#')

#selecting 'View Results' button
resultsElem = browser.find_element_by_class_name("btn.btn-submit.js-search-form-submit").click()

我收到以下错误:

  

NoSuchElementException:消息:无法找到元素:   {“方法”:“类   名称”, “选择器”: “btn.btn-submit.js搜索表单提交”}

我试过find_element_by_class_name

我试过find_element_by_name

我试过find_element_by_id

我试过find_element_by_link_text

但没有成功。有什么建议。非常感谢。

加文。

1 个答案:

答案 0 :(得分:1)

class_name收到一个班级名称

browser.find_element_by_class_name("btn")
# or
browser.find_element_by_class_name("btn-submit")
# or
browser.find_element_by_class_name("js-search-form-submit")

如果您希望所有课程一起使用css_selector

browser.find_element_by_css_selector(".btn.btn-submit.js-search-form-submit")

您也无法将结果分配给变量并在同一时间单击它。它应该是

resultsElem = browser.find_element_by_class_name("btn")
# or
browser.find_element_by_class_name("btn").click()