【问题标题】:Clicking a button with Selenium in Python在 Python 中单击带有 Selenium 的按钮
【发布时间】: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


【解决方案1】:

看来您只是使用了不正确的选择器。

注意

  • div中的@id"ember1002"一样是动态值,所以每次访问页面都会不同:"ember1920""ember1202"等...

    李>
  • find_element_by_link_text() 只能应用于链接,例如&lt;a&gt;Companies&lt;/a&gt;,但不是按钮

尝试通过文本内容找到按钮:

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

【讨论】:

    【解决方案2】:

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

    page.click_button("Companies")
    

    奖励:这将对按钮实​​现中的更改具有弹性,例如,使用 &lt;input type="submit" /&gt; 等。面对按钮出现之前的延迟,它也将具有弹性,因为 click_button() 将等待它是可见和启用的。

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2020-04-14
      • 2022-01-19
      • 1970-01-01
      • 2015-10-18
      • 2020-08-26
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多