在Python中单击带有Selenium的按钮 [英] Clicking a button with Selenium in Python

查看:145
本文介绍了在Python中单击带有Selenium的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用Selenium和Python在LinkedIn的搜索栏中搜索公司名称,然后单击导航中的公司"按钮,以获取与该关键字相似的公司信息(而不是比该公司的个人).请参阅下面的示例. "CalSTRS"是我在搜索栏中搜索的公司.然后,我想单击公司"导航按钮.

Goal: use Selenium and Python to search for company name on LinkedIn's search bar THEN click on the "Companies" button in the navigation to arrive to information about companies that are similar to the keyword (rather than individuals at that company). See below for an example. "CalSTRS" is the company I search for in the search bar. Then I want to click on the "Companies" navigation button.

我的助手功能:我定义了以下助手功能(包括此处的可重复性).

My Helper Functions: I have defined the following helper functions (including here for reproducibility).

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:

Here is the HTML of the "Companies" button element:

<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的了解不是很丰富,所以我可能缺少明显的东西.显然,我不是在选择正确的元素或与之交互.到目前为止,我已经尝试过...

What I have tried: Admittedly, I am not very experienced with HTML and CSS so I am probably missing something obvious. Clearly, I am not selecting / interacting with the right element. So far, I have tried...

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()

这将返回此回溯...

which returns this traceback...

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)

推荐答案

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

It seem that you simply used incorrect selectors.

请注意

    div中的
  • @id(如"ember1002")是动态值,因此每次访问页面时都会有所不同:"ember1920""ember1202"等...

  • @id of div like "ember1002" is dynamic value, so it will be different each time you visit page: "ember1920", "ember1202", etc...

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

find_element_by_link_text() can be applied to links only, e.g. <a>Companies</a>, but not buttons

尝试按其文本内容查找按钮:

Try to find button by its text content:

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

这篇关于在Python中单击带有Selenium的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆