【发布时间】:2018-02-07 20:36:33
【问题描述】:
目标:使用 Selenium 和 Python 在 LinkedIn 的搜索栏上搜索公司名称 然后单击导航中的“公司”按钮以到达与关键字相似的公司信息(而不是比该公司的个人)。请参阅下面的示例。 “CalSTRS”是我在搜索栏中搜索的公司。然后我想点击“公司”导航按钮。
我的辅助函数:我已经定义了以下辅助函数(包括这里是为了重现性)。
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)
【问题讨论】:
-
这可能是个问题?我想我正在使用复合类。见THIS POST。我不确定如何更改上面的 Xpath
-
那么您的问题是什么?你有什么异常吗?
-
@Andersson 代码似乎没有选择任何要操作的元素。是的,我正在收到回溯。我已将它们包括在上面
标签: python-3.x selenium selenium-webdriver selenium-chromedriver