gpt4 book ai didi

python - 登录销售导航器 python selenium

转载 作者:行者123 更新时间:2023-12-01 01:08:13 25 4
gpt4 key购买 nike

我正在尝试实现登录销售导航页面的简单任务,之后我可以继续尝试抓取潜在客户和帐户数据,作为抓取练习。继此link为了登录,我有这个脚本来完成任务

    browser = webdriver.Firefox(executable_path=r'D:\geckodriver\geckodriver.exe')

def login_linkedin(email_id,psswd):

sales_url = "https://www.linkedin.com/sales/login"
browser.get(sales_url)

elem_username = browser.find_element_by_id("username")
#elem_username = browser.find_element_by_name("session_key")
elem_username.send_keys(email_id)
elem_psswd = browser.find_element_by_id("password")
#elem_psswd = browser.find_element_by_name("session_password")
elem_psswd.send_keys(psswd)

elem_signin = find_element_by_class_name("login__form_action_container ").click()

if __name__ == "__main__":

login_linkedin("la","la")

通过检查用户名、密码和登录按钮字段的每个元素(如下所示),我尝试按名称、类名和 id 查找元素,但最终总是找不到 元素错误。我可能会错过什么?

用户名元素

  <input id="username" name="session_key" type="text" aria-describedby="error-for-username" required="" validation="email|tel" autofocus="" aria-label="Email or Phone">

密码元素

  <input id="password" type="password" aria-describedby="error-for-password" name="session_password" required="" validation="password" aria-label="Password">

登录按钮元素

  <button class="btn__primary--large from__button--floating" type="submit" aria-label="Sign in">Sign in</button>

错误

Traceback (most recent call last):
File "D:\Freelancing\Scrape_sales_nav\code\my_code\scrape_accounts.py", line 40, in <module>
login_linkedin("la","la")
File "D:\Freelancing\Scrape_sales_nav\code\my_code\scrape_accounts.py", line 21, in login_linkedin
elem_username = browser.find_element_by_id("username")
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="username"]

最佳答案

问题是您要查找的内容是在 iFrame 中,因此我们需要将 selenium 指向 iframe 而不是标准网页。幸运的是,selenium 有一个函数:switch_to_frame()

解决方案:

import time

browser = webdriver.Firefox(executable_path=r'D:\geckodriver\geckodriver.exe')

def login_linkedin(email_id, psswd):
sales_url = "https://www.linkedin.com/sales/login"
browser.get(sales_url)
# sleeping to give the page time to load
time.sleep(1)
# the two lines below will put us into the iframe
frame = browser.find_element_by_tag_name('iframe')
browser.switch_to_frame(frame)
# entering the username and password
elem_username = browser.find_element_by_id("username")
elem_username.send_keys(email_id)
elem_psswd = browser.find_element_by_id("password")
elem_psswd.send_keys(psswd)
# no need for a variable name, we can just click
browser.find_element_by_class_name("login__form_action_container ").click()

if __name__ == "__main__":
login_linkedin('email_id', 'psswd')

关于python - 登录销售导航器 python selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55128400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com