我尝试编写的程序遇到一点障碍。我希望它去linkedin并搜索预定义的搜索词,然后去每个人与他们联系。我无法使程序单击每个按钮。知道我要去哪里错了吗?我一直收到回溯错误,指出当时无法点击。
from selenium import webdriver
import time
from bs4 import BeautifulSoup as bs
url = 'https://www.linkedin.com'
username = 'YOUR USERNAME'
password = 'YOUR PASSWORD'
search = 'fishing in alabama'.replace(' ', '%20').strip()
driver = webdriver.Chrome()
driver.get(url)
driver.set_window_size(1400, 1400)
driver.maximize_window()
user = driver.find_element_by_class_name('login-email')
passw = driver.find_element_by_class_name('login-password')
user.send_keys(username)
passw.send_keys(password)
time.sleep(.5)
driver.find_element_by_id('login-submit').click()
# Add Loop to Go through different pages.
url = 'https://www.linkedin.com/search/results/people/v2/?keywords=' + search + '&origin=GLOBAL_SEARCH_HEADER&page=' + str(count)
print(url)
driver.get(url)
for num in range(1,6):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
print('Clicked ' + str(num) + ' times')
time.sleep(.0125)
soup = bs(driver.page_source, 'lxml')
results = soup.findAll('li', class_='search-result search-result__occluded-item ember-view')
for button in soup.findAll('button', class_='search-result__actions--primary button-secondary-medium m5'):
#If the button text contains "Connect" then I want it to click the button.
if button.text.strip() == 'Connect':
print(button.text)
buttons = driver.find_element_by_class_name('search-result__actions--primary')
buttons.click()