【问题标题】:Using Selenium with Python on LinkedIn在 LinkedIn 上使用 Selenium 和 Python
【发布时间】:2019-02-06 21:26:53
【问题描述】:

我正在使用 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 行 按钮= 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) 文件“/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(response) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py”, 第 242 行,在 check_response 中 raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to 定位元素://button[contains(@aria-label,"Send message to")]

还有什么我没有想到的吗?

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

【问题讨论】:

  • 与定位器、步骤共享代码并尝试添加等待
  • 干杯!我在所有地方都添加了等待。如果我输入: page.find_all('button') 我会得到一个列表,但是当我通过 driver.find_element_by 执行此操作时,我总是会收到上面的错误
  • 我在我的回答中给了你需要的代码
  • 谢谢你,伙计!知道为什么我得到:名称'By'未定义错误吗?除了你的我错过了任何进口吗?

标签: python python-3.x selenium bots linkedin


【解决方案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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 是的,我做到了。现在得到以下错误错误:connectionsHeader = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".message-anywhere-button"))).text File "/Library/Frameworks/Python.framework/Versions/3.7/ lib/python3.7/site-packages/selenium/webdriver/support/wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: The message is empty但我将时间从 20 改为 3 改为 10。我想真正的问题是它无法通过 css 选择器找到元素...
    • 再检查一下,我用的是.mn-connections__header h2而不是.message-anywhere-button
    • 是的,我尝试了你的课程和我在我所在页面上找到的课程,但没有成功。总是超时错误。 time.sleep() 不会引发该错误,但我稍后仍然会遇到相同的问题,即没有找到任何元素。不知道如何摆脱它......
    • 你甚至没有尝试。有一个巨大的错误,你无法运行它。
    猜你喜欢
    • 2022-08-22
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2022-08-18
    • 1970-01-01
    • 2021-08-22
    • 2021-12-18
    相关资源
    最近更新 更多