使用 selenium python 在我的代码中进行注释后脚本未运行

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

我正在尝试使用硒来自动化链接。 我想搜索记者并想将他们全部连接起来。 因此,我编写了单击所有连接按钮的代码。

但它不起作用。

from selenium import webdriver
import time

driver = webdriver.Chrome('C:\\Users\\SUJASH\\AppData\\Local\\Temp\\HZ$D.705.3782\\HZ$D.705.3783\\chromedriver.exe')
driver.get('https://www.linkedin.com')
time.sleep(2)

#********** LOG IN *************

username = driver.find_element("id", "session_key")
password = driver.find_element("id", "session_password")

#上面的代码会找到用户名和密码框

username.send_keys('HIDDEN_ON_SOF')
password.send_keys('HIDDEN_ON_SOF')
time.sleep(2)

#以上代码将输入用户名和密码

submit = driver.find_element("xpath", "//button[@type='submit']").click()

#上面的代码将点击提交按钮 #************ 搜索 **********

driver.get('https://www.linkedin.com/search/results/people/?heroEntityKey=urn%3Ali%3Aautocomplete%3A1975299068&keywords=entertainment%20journalist&origin=CLUSTER_EXPANSION&sid=uv!')
time.sleep(5)

'''上面的代码有我想要搜索的链接,我想搜索娱乐记者,所以我搜索了娱乐记者并复制链接并粘贴到这里'''

#************ from here script is not working ****************************

all_buttons = driver.find_elements("tag_name", "button")
connect_buttons = [btn for btn in all_buttons if btn.text == "Connect"]

for btn in connect_buttons:
    driver.execute_script("arguments[0].click();", btn)
    time.sleep(2)
    send = driver.find_element("xpath" , "//button[@aria-label='Send now']")
    driver.execute_script("arguments[0].click();", send)
    close = driver.find_element("xpath" , "//button[@aria-label='Dismiss']")
    driver.execute_script("arguments[0].click();", close)
    time.sleep(2)

''' 上面的代码不起作用,我想单击所有连接按钮,但在注释后代码不起作用 *** 来自此处的脚本不起作用 *** '''

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

首先,你使用

time.sleep
等待LinkedIn回复,这总是有点冒险。最好编写一些等待操作完成的代码。

但是,如果您要使用

time.sleep
,那么您就错过了单击“发送”和单击“关闭”之间的睡眠时间。这可能会扰乱你的循环。

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