单击Python中的Selenium按钮

时间:2018-02-07 20:36:34

标签: python-3.x selenium selenium-webdriver selenium-chromedriver

目标:使用Selenium和Python在LinkedIn的搜索栏中搜索公司名称然后点击"公司"导航中的按钮可以获得与关键字类似的公司的信息(而不是该公司的个人)。请参阅下面的示例。 "&加州教师退休基金#34;是我在搜索栏中搜索的公司。然后我想点击"公司"导航按钮。

LinkedIn list navigation

我的助手功能:我已经定义了以下辅助函数(包括此处的可重复性)。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from random import randint
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()

def li_bot_login(usrnm, pwrd):
    ##-----Log into linkedin and get to your feed-----
    browser.get('https://www.linkedin.com')

    ##-----Find the Search Bar-----
    u = browser.find_element_by_name('session_key')
    ##-----Enter Username and Password, Enter-----
    u.send_keys(usrnm)
    p = browser.find_element_by_name('session_password')
    p.send_keys(pwrd + Keys.ENTER)

def li_bot_search(search_term):
    #------Search for term in linkedin search box and land you at the search results page------
    search_box = browser.find_element_by_css_selector('artdeco-typeahead-input.ember-view > input')
    search_box.send_keys(str(search_term) + Keys.ENTER)

def li_bot_close():
    ##-----Close the Webdriver-----
    browser.close()

li_bot_login()
li_bot_search('calstrs')
time.sleep(5)
li_bot_close()

以下是"公司"的HTML。按钮元素:

<button data-vertical="COMPANIES" data-ember-action="" data-ember-action-7255="7255">
      Companies
    </button>

XPath:

//*[@id="ember1202"]/div[5]/div[3]/div[1]/ul/li[5]/button

我尝试了什么:不可否认,我对HTML和CSS不是很有经验,所以我可能错过了一些明显的东西。显然,我没有选择/与正确的元素互动。到目前为止,我已经尝试过......

companies_btn = browser.find_element_by_link_text('Companies')
companies_btn.click()

返回此回溯:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Companies"}
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)

companies_btn_xpath = '//*[@id="ember1202"]/div[5]/div[3]/div[1]/ul/li[5]/button'
browser.find_element_by_xpath(companies_btn_xpath).click()

有这个追溯......

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ember1202"]/div[5]/div[3]/div[1]/ul/li[5]/button"}
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)

browser.find_element_by_css_selector('#ember1202 > div.application-outlet > div.authentication-outlet > div.neptune-grid.two-column > ul > li:nth-child(5) > button').click()

返回此追溯...

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#ember1202 > div.application-outlet > div.authentication-outlet > div.neptune-grid.two-column > ul > li:nth-child(5) > button"}
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)

2 个答案:

答案 0 :(得分:3)

您似乎只是使用了错误的选择器。

请注意

    @id div的{​​{1}} "ember1002"是动态值,因此每次访问页面时都会有所不同:"ember1920""ember1202",等...

  • find_element_by_link_text()只能应用于链接,例如<a>Companies</a>,但不是按钮

尝试按文字内容查找按钮:

browser.find_element_by_xpath('//button[normalize-space()="Companies"]').click()

答案 1 :(得分:2)

使用capybara-py(可以用来驱动Selenium),这很简单:

page.click_button("Companies")

奖励:这将对按钮实​​现的变化具有弹性,例如,使用<input type="submit" />等。面对按钮出现之前的延迟,它也具有弹性,如{{1}将等待它可见并启用。