无法在 LinkedIn 自动化中单击查找并单击按钮:Python-Selenium

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

我一直在尝试构建一个小 python-Selenium 脚本来帮助我自动化 linkedIn 网络。我希望我的脚本能够遍历预定义的搜索条件并向每个结果发送连接请求。我将坚持公平使用,因为没有人喜欢滥用这些东西,但当你可以自动化它时,谁有时间花一整天的时间在 linkedIn 上!这些天我是一个非常休闲的程序员,并且对我所拥有的东西感到满意,因为我是 Python-Selenium 的新手。我有打开 linkedin 的脚本,登录并查找符合我的条件的配置文件。我遇到的问题是我似乎无法让 Selenium 找到或单击“连接”按钮。有谁知道我在这里做错了什么,或者我做得对吗,但 Linkedin 机器人检查对于此类事情来说太好了,我不应该浪费时间?

提前致谢。

# Iterate through each profile link and perform actions
for link in profile_links:
    if profiles_processed >= rate_limit:
        print(f"Rate limit of {rate_limit} profiles reached. Stopping the script.")
        break

    try:
        # Open the profile in a new tab
        profile_url = link.get_attribute("href")
        driver.execute_script("window.open(arguments[0], '_blank');", profile_url)
        driver.switch_to.window(driver.window_handles[1])  # Switch to the new tab
        time.sleep(3)

        # Step 1: Try to find and click the 'Connect' button based on the Linkedin 'connect button' HTML 
        try:
            connect_button = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label, 'Invite') and contains(@class, 'artdeco-button--primary')]"))
            )
            connect_button.click()
            print("Clicked 'Connect' on the profile page.")
        except TimeoutException:
            print("Couldn't find 'Connect' button on the profile page, skipping this profile.")
            driver.close()
            driver.switch_to.window(driver.window_handles[0])  # Switch back to the original tab
            continue

        # Step 2: Handle the modal (wait for it to appear and click 'Send without a note')
        try:
            send_without_note_button = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label, 'Send without a note')]"))
            )
            send_without_note_button.click()
            print("Connection request sent without a note.")
        except TimeoutException:
            print("Failed to send the connection request due to modal issue.")

        # After connecting or failing, close the tab and switch back to the search results tab
        driver.close()
        driver.switch_to.window(driver.window_handles[0])  # Switch back to the original tab

        profiles_processed += 1

        # Randomized wait before moving to the next profile
        time.sleep(random.randint(10, 20))  # Random delay between 10 to 20 seconds

    except Exception as e:
        print(f"Unexpected error: {e}. Skipping this profile.")
        driver.close()  # Close the tab if there was an error
        driver.switch_to.window(driver.window_handles[0])  # Switch back to the original tab
        continue
python selenium-webdriver automation
1个回答
0
投票

总的来说,正如您所说:

Linkedin Bot checks are just too good for this
。但让我们尝试一下吧。

但是为了提供帮助,您必须定义可以单击的所有类型的按钮。每次访问时显示的按钮都会发生变化。在这种情况下,还有一个用于跟随而不是连接的选项(artdeco-button--secondary),并且连接按钮并不总是显示。有时你必须点击更多..按钮。

不过,这可能有点像兔子洞。作为一般经验法则,如果您可以支付 API 访问费用,则可以确定有人已经设计出一种方法来防止或减少机器人流量。但你总是可以尝试!

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