使用Python中的Selenium点击按钮

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

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

它返回这个回溯(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)

也许这可能是个问题?我想我正在使用一个复合类。请参见此帖子。我不确定如何更改上面的Xpath。 - Merv Merzoug
那么你的问题是什么?你有遇到任何异常吗? - Andersson
@Andersson 目前的代码似乎没有选择任何要操作的元素。是的,我收到了回溯信息。我已经在上面包含了它们。 - Merv Merzoug
2个回答

8
似乎您使用了错误的选择器。
请注意:
- div 的 @id(例如 "ember1002")是动态值,因此每次访问页面时都会不同:如 "ember1920"、"ember1202" 等。 - find_element_by_link_text() 只能应用于链接,例如 Companies,而不能应用于按钮。
尝试通过其文本内容查找按钮。
browser.find_element_by_xpath('//button[normalize-space()="Companies"]').click()

2
使用 capybara-py(可用于驱动Selenium),这很容易实现:
page.click_button("Companies")

奖励:这将对按钮实现的更改具有弹性,例如使用<input type="submit" />等。它还将在按钮出现之前面对延迟时具有弹性,因为click_button()将等待其可见并启用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接