我使用 python 的 Selenium seesion 无法单击按钮

问题描述 投票:0回答:1

嘿,我是 selenium 的新手,我正在尝试搜索我的 linkedin 个人资料,让我进入该个人资料并允许我单击登录按钮,这是代码(其中一部分来自 chatgpt)

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Initialize the Chrome driver
driver = webdriver.Chrome()

try:
    # Maximize the browser window
    driver.maximize_window()

    # Open Google
    driver.get("https://www.google.com/")
    print("Opened Google.")

    # Locate the search bar and search for"my name Linkedin"
    element = driver.find_element(By.NAME, "q")
    element.clear()
    element.send_keys("my name Linkedin")
    element.send_keys(Keys.RETURN)
    print("Searched for 'my name Linkedin'.")

    # Wait for the search results to appear and click the LinkedIn link
    linkedin_link = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//a[contains(@href, 'linkedin.com')]"))
    )
    linkedin_link.click()
    print("Clicked on LinkedIn link.")

    # Wait for the LinkedIn page to load and click the sign-in button
    sign_in_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'sign-in-modal__outlet-btn cursor-pointer btn-md btn-primary')]"))
    )
    sign_in_button.click()
    print("Clicked on 'Sign in' button.")

    # Verify the sign-in button is clicked by checking for the sign-in page elements
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "username"))
    )
    print("Sign-in page loaded successfully.")

except Exception as e:
    print("An error occurred:", e)
finally:
    # Wait for a while to see the result before quitting
    time.sleep(40)
    driver.quit()

我尝试的另一种方法非常基本,但效果不佳(我根据我的理解写的)

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get("https://www.google.com/")
# to locate any element by its class name on the webpage
element = driver.find_element(By.CLASS_NAME,"gLFyf")
# to clear ant already present text
element.clear() 
#search for something on the search bar of the google webpage
element.send_keys("my name Linkedin")
element.send_keys(Keys.RETURN)
element = driver.find_element(By.CLASS_NAME,"VuuXrf")
element.click()
element = driver.find_element(By.CLASS_NAME,"sign-in-modal__outlet-btn cursor-pointer btn-md btn-primary")
element.click()
# element                           
time.sleep(40)
driver.quit()

谁能告诉我代码中的错误吗?

我试图让 selenium 会话单击 linkedin 上的登录按钮,但它不会这样做

python selenium-webdriver selenium-chromedriver
1个回答
0
投票

检查下面的工作代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("https://www.google.com/")
# Click on Accept all cookies
driver.find_element(By.XPATH, "//div[text()='Accept all']").click()
el = driver.find_element(By.CLASS_NAME,"gLFyf")
el.send_keys("my name Linkedin")
el.send_keys(Keys.RETURN)
driver.find_element(By.CLASS_NAME,"VuuXrf").click()
driver.find_element(By.XPATH,"//a[text()='Sign in']").click()
time.sleep(15)
driver.quit()

代码中的问题:

  • 一旦您通过 selenium 启动 google,它就会在干净的浏览器配置文件中打开(类似于隐身模式)。您会注意到出现一个 cookie 弹出窗口。在继续采取进一步行动之前,您需要摆脱它。

  • 以下代码不正确。当有多个类时,您不能使用

    CLASS_NAME
    定位器策略。

    element = driver.find_element(By.CLASS_NAME,"sign-in-modal__outlet-btn cursor-pointer btn-md btn-primary")
    

建议:

  • 使用selenium的waits来有效地定位元素。您可以使用

    implicit
    explicit
    等待。我最喜欢的等待是
    explicit waits
    。我在答案中使用了
    implicit wait

  • 您不必始终将元素存储在变量中。仅当您想在以下代码中多次使用它时,才将其存储在变量中。例如,更改以下代码,

    element = driver.find_element(By.CLASS_NAME,"VuuXrf")
    element.click()
    

    到,

    driver.find_element(By.CLASS_NAME,"VuuXrf").click()       
    
© www.soinside.com 2019 - 2024. All rights reserved.