在LinkedIn上使用Selenium和Python

在LinkedIn上使用Selenium和Python,python,python-3.x,selenium,bots,linkedin,Python,Python 3.x,Selenium,Bots,Linkedin,我正在使用python和selenium构建一个脚本,单击网络上的“消息按钮”发送默认消息 Linkedin使用动态字段ember,因此无法按id查找元素。到目前为止,我已尝试: driver.find_elements_by_class_name("...").click() driver.find_element_by_tag_name("button").click() driver.find_element_by_css_selector("...").click() driver.fi

我正在使用python和selenium构建一个脚本,单击网络上的“消息按钮”发送默认消息

Linkedin使用动态字段ember,因此无法按id查找元素。到目前为止,我已尝试:

driver.find_elements_by_class_name("...").click()
driver.find_element_by_tag_name("button").click()
driver.find_element_by_css_selector("...").click()
driver.find_element_by_xpath(//...)
这是我目前的代码:

def TextBot(browser):
time.sleep(3)
browser.get('https://www.linkedin.com/mynetwork/invite-connect/connections/')
time.sleep(3)
xpath = '//button[contains(@aria-label,"Send message to")]'
time.sleep(3)
buttons = driver.find_element_by_xpath(xpath)
for btn in buttons:
    print("Can %s" % btn.get_attribute("aria-label"))  

def Main():
#Parse enail and password to the script
parser = argparse.ArgumentParser()
parser.add_argument('email', help='linkedin email')
parser.add_argument('password', help='linkedin password')
args = parser.parse_args()

#browse to the login page
browser = webdriver.Firefox()
browser.get('https://linkedin.com/uas/login')

#Parse the two argument in the login form
emailElement = browser.find_element_by_id('session_key-login')
emailElement.send_keys(args.email)
passElement = browser.find_element_by_id('session_password-login')
passElement.send_keys(args.password)
passElement.submit()

#Initialise ViewBot function
os.system('clear') #cls rather than clear on windows
print ("[+] Success! Logged In, Bot Starting")
#ViewBot(browser)
TextBot(browser)
browser.close()
我的错误是:

TextBot中的文件LinkedInBot.py,第88行 buttons=driver.find_element_by_XPath File/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py, 第393行,按xpath查找元素 返回self.find_elementby=By.XPATH,value=XPATH文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py, 第966行,在find_元素中 “值”:value}['value']文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py, 第320行,执行中 self.error\u handler.check\u响应文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py, 第242行,在check_响应中 引发异常\u类消息,屏幕,堆栈跟踪selenium.common.exceptions.NoTouchElementException:消息:无法 定位元素://按钮[contains@aria-标签,将消息发送到]

还有什么我没想到的吗


顺便说一句,谢谢你的回答。我很接近,但我仍然认为Linkedin中的消息按钮可以绕过一些其他形式的加密,如下所示:

<button class="message-anywhere-button mn-connection-card__message-btn button-secondary-medium" aria-label="Send message to John Smith" data-ember-action="" data-ember-action-5128="5128">
  <span aria-hidden="true">Message</span>
  <span class="visually-hidden">
    Send a message to John Smith
  </span>
</button>
此定位器将查找所有按钮。但根据您调用的函数,最终可能只选择第一个元素,或者选择所有元素。假设目标是收集所有按钮:

xpath = '//button[contains(@aria-label,"Send message to")]'
all_message_buttons = driver.find_elements(By.XPATH, xpath)
for message_button in all_message_buttons:
    print("Can %s" % message_button.get_attribute("aria-label")) 
    # prints Can Send Message to John Smith
    # and any other names available on page
最后,在选择按钮之前,您需要确保页面已加载,并且确实显示了按钮。有多种方法可以做到这一点,但我通常只是将查找所需元素替换为等待它们:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ...
xpath = '//button[contains(@aria-label,"Send message to")]'
wait = WebDriverWait(browser, 10) # wait for up to 10 sec
all_message_buttons = wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
for message_button in all_message_buttons:
    print("Can %s" % message_button.get_attribute("aria-label")) 
    # prints Can Send Message to John Smith
    # and any other names available on page

下面的代码向下滚动,直到所有联系人都已加载。 然后获取所有消息按钮,单击、发送并关闭消息窗口


不是:代码可能包含打字错误或小错误。请随意改进它

与定位器、步骤共享您正在使用的代码,并尝试添加服务员!我到处都在等待。如果我键入:page.find_all“按钮”,我会得到一个列表,但当我通过driver.find_element_by这样做时,我总是会得到上面的错误,我在下面的回答中给了你需要的代码谢谢你,伙计!知道我为什么会得到:name'By'没有定义错误吗?除了你的,我还错过了什么?是的,我错过了。现在出现以下错误错误:connectionHeader=wait.untelec.visibility\u of_element\u locatedBy.CSS\u SELECTOR、.message-anywhere-button.text文件/Library/Frameworks/Python.framework/Frameworks/3.7/site-packages/selenium/webdriver/support/wait.py,第80行,在直到raise TimeoutExceptionmessage屏幕中,stacktrace selenium.common.exceptions.TimeoutException:Message:消息为空,但我将时间从20改为3改为10。我想真正的问题是它无法通过css选择器找到元素…再次检查,我使用了.mn-connections\uuu header h2 not.message anywhere按钮是的,我尝试了你的类和我在页面中找到的类,但没有成功。总是超时错误。sleep没有抛出那个错误,但我以后仍然会遇到同样的问题,没有找到任何元素。不知道如何摆脱它…你甚至都没试过。有一个巨大的错误,你不能运行它。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ...
xpath = '//button[contains(@aria-label,"Send message to")]'
wait = WebDriverWait(browser, 10) # wait for up to 10 sec
all_message_buttons = wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
for message_button in all_message_buttons:
    print("Can %s" % message_button.get_attribute("aria-label")) 
    # prints Can Send Message to John Smith
    # and any other names available on page
from selenium.webdriver.support import expected_conditions as EC
import re

#...

wait = WebDriverWait(driver, 20)
connectionsHeader = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".mn-connections__header h2"))).text
totalConnections = int(re.findall(r"\d+", connectionsHeader))
while len(driver.find_elements_by_css_selector(".mn-connections li")) < totalConnections-1:
    driver.execute_script("window.scrollTo(0, 100);")

messageButtons = driver.find_elements_by_css_selector(".mn-connections li button.mn-connection-card__message-btn")

for button in messageButtons:
    button.click()
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".msg-form__contenteditable"))).click()
    driver.find_elements_by_css_selector(".msg-form__contenteditable").send_keys('message')
    driver.find_elements_by_css_selector(".js-msg-close").click()
    wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, ".js-msg-close")))