首页 文章

在LinkedIn上使用Selenium和Python

提问于
浏览
-1

我正在使用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()

而我的错误:

文件“LinkedInBot.py”,第88行,TextBot按钮= driver.find_element_by_xpath(xpath)文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/ remote / webdriver.py“,第393行,在find_element_by_xpath中返回self.find_element(by = By.XPATH,value = xpath)File”/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- packages / selenium / webdriver / remote / webdriver.py“,第966行,在find_element'value':value})['value']文件”/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /site-packages/selenium/webdriver/remote/webdriver.py“,第320行,执行self.error_handler.check_response(响应)文件”/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ site-packages / selenium / webdriver / remote / errorhandler.py“,第242行,在check_response中引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:无法找到element:// button [contains( @ aria-label,“发给我ssage to“)]

还有什么我没想到的吗?

顺便感谢到目前为止的答案 . 我很接近,但我仍然认为还有其他形式的加密可以绕过

2 回答

  • 1

    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>
    

    所以对于定位器你有几个选项,让我们解决最原始的问题:在 aria-label 中查找 Send message to 文本:

    xpath = '//button[contains(@aria-label,"Send message to")]'
    

    该定位器将找到所有按钮 . 但是,根据您调用的函数,您可能最终只选择第一个元素或所有元素 . 让我们说目标是收集所有按钮:

    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
    
  • 0

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

    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")))
    

    不是:代码可能包含拼写错误或小错误 . 随意改进它 .

相关问题