簡體   English   中英

我在我的代碼中使用了 Selenium,但我一直收到錯誤。 誰能幫忙?”

[英]I'm using Selenium in my code, but I keep getting an error. Can anyone help?"

基本上,每當我運行我的代碼時,它總是在它說的部分出錯

username_field = login_form.find_element(By.NAME, 'login_email')

這是完整的源代碼

from selenium import webdriver
from selenium.webdriver.common.by import By

# Set the path to the Chrome driver executable
chrome_driver_path = '/path/to/chromedriver'

# Create a new Chrome webdriver
driver = webdriver.Chrome(chrome_driver_path)

# Navigate to the PayPal login page
driver.get('https://www.paypal.com/signin')

# Find all elements with the class 'login-form'
login_form_elements = driver.find_element(By.CLASS_NAME, 'contentContainer')

# Get the first element from the list
login_form = login_form_elements

# Find the username field inside the login form
username_field = login_form.find_element(By.NAME, 'login_email')

# Find the password field inside the login form
password_field = login_form.find_element(By.NAME, 'login_password')

# Fill in the username and password
username_field.send_keys('your_username')
password_field.send_keys('your_password')

# Find the login button and click it
login_button = login_form.find_element_by_id('btnLogin')
login_button.click()

我什么都試過了還是不行

find_element() 方法應該返回單個元素,但看起來您正試圖將結果存儲在一個名為 login_form_elements 的變量中,該變量用於保存元素列表。

要解決此問題,您應該使用 find_elements() 方法而不是 find_element()。

這樣,您可以找到符合給定條件的多個元素。

from selenium import webdriver
from selenium.webdriver.common.by import By

# Set the path to the Chrome driver executable
chrome_driver_path = '/path/to/chromedriver'

# Create a new Chrome webdriver
driver = webdriver.Chrome(chrome_driver_path)

# Navigate to the PayPal login page
driver.get('https://www.paypal.com/signin')

# Find all elements with the class 'login-form'
login_form_elements = driver.find_elements(By.CLASS_NAME, 'contentContainer')

# Get the first element from the list
login_form = login_form_elements[0]

# Find the username field inside the login form
username_field = login_form.find_element(By.NAME, 'login_email')

# Find the password field inside the login form
password_field = login_form.find_element(By.NAME, 'login_password')

# Fill in the username and password
username_field.send_keys('your_username')
password_field.send_keys('your_password')

# Find the login button and click it
login_button = login_form.find_element_by_id('btnLogin')
login_button.click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM