通过Selenium和Python点击LinkedIn中的“关注按钮”

问题描述 投票:0回答:1

我开始学习 Selenium,要完成的任务之一是单击位于右侧底部的“关注”按钮,特别是在“关于公司”部分。

HTML Inspect

登录后,我尝试使用这两个定位器来查找元素:

定位器 XPATH

follow_button = driver.find_element(By.XPATH, value='//*[@id="main"]/div/div[2]/div[2]/div/div[2]/div/div[1]/div/section/section/div[1]/div[1]/button')
follow_button.click()

定位器CSS_SELECTOR

follow_button = driver.find_element(By.CSS_SELECTOR, value='.jobs-company__box div button')
follow_button.click()

follow_button = driver.find_element(By.CSS_SELECTOR, value='.jobs-company div div button')
follow_button.click()

我无法点击“Follow”按钮,Python 只是向我抛出了他的消息错误:

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\LinkedIn\main.py", line 31, in <module>
    follow_button.click()
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 94, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (910, 2211)
  (Session info: chrome=123.0.6312.105)
Stacktrace:
    GetHandleVerifier [0x00007FF7CBB17072+63090]
    (No symbol) [0x00007FF7CBA82CC2]
    (No symbol) [0x00007FF7CB91EC65]
    (No symbol) [0x00007FF7CB96BB34]
    (No symbol) [0x00007FF7CB969954]
    (No symbol) [0x00007FF7CB967164]
    (No symbol) [0x00007FF7CB965EF9]
    (No symbol) [0x00007FF7CB95A708]
    (No symbol) [0x00007FF7CB986FDA]
    (No symbol) [0x00007FF7CB95A00A]
    (No symbol) [0x00007FF7CB9871F0]
    (No symbol) [0x00007FF7CB9A3412]
    (No symbol) [0x00007FF7CB986D83]
    (No symbol) [0x00007FF7CB9583A8]
    (No symbol) [0x00007FF7CB959441]
    GetHandleVerifier [0x00007FF7CBF125CD+4238285]
    GetHandleVerifier [0x00007FF7CBF4F72D+4488493]
    GetHandleVerifier [0x00007FF7CBF47A0F+4456463]
    GetHandleVerifier [0x00007FF7CBBF05B6+953270]
    (No symbol) [0x00007FF7CBA8E58F]
    (No symbol) [0x00007FF7CBA89264]
    (No symbol) [0x00007FF7CBA8939B]
    (No symbol) [0x00007FF7CBA79BD4]
    BaseThreadInitThunk [0x00007FFDBC227344+20]
    RtlUserThreadStart [0x00007FFDBDDA26B1+33]

但是,当应用 XPATH 定位器单击“保存”按钮时,我完全没有问题。这是完整的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.linkedin.com/jobs/search/?f_LF=f_AL&geoId=102257491&keywords=python%20developer&location=London%2C%20England%2C%20United%20Kingdom&redirect=false&position=1&pageNum=0")

time.sleep(2)
sign_in = driver.find_element(By.LINK_TEXT, value="Sign in")
sign_in.click()

time.sleep(1)
user_name = driver.find_element(By.ID, value="username")
user_name.send_keys("YOUR_USERNAME")
last_name = driver.find_element(By.ID, value="password")
last_name.send_keys("YOUR_PASSWORD")
sign_in_button = driver.find_element(By.CSS_SELECTOR, value="form div button")
sign_in_button.click()

input("Press Enter after Captcha is finished")

time.sleep(2)
save_button = driver.find_element(By.XPATH, value='//*[@id="main"]/div/div[2]/div[2]/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div[1]/div[4]/div/button/span[1]')
save_button.click()

time.sleep(2)
**follow_button = driver.find_element(By.XPATH, value='//*[@id="main"]/div/div[2]/div[2]/div/div[2]/div/div[1]/div/section/section/div[1]/div[1]/button/span')
follow_button.click()**

我想要获取的元素截图: enter image description here

任何建议都将受到高度赞赏。 谢谢

python selenium-webdriver linkedin-api
1个回答
0
投票

出现 ElementClickInterceptedException 的原因之一可能是当您尝试单击“跟随”按钮时,该按钮不在视口中。向下滚动之前该元素不可见。

正如我所看到的,“已保存”按钮位于页面顶部,这意味着它始终位于视口中,但是对于“关注”按钮,您向下滚动了吗?

从您发布的图片中很难确定您是否滚动,因此请根据您的情况考虑我的答案。

https://www.selenium.dev/documentation/webdriver/actions_api/wheel/#scroll-to-element

© www.soinside.com 2019 - 2024. All rights reserved.