python-3.x Selenium不会将数据

lokaqttq  于 2023-03-09  发布在  Python
关注(0)|答案(2)|浏览(144)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options = options, service = Service(ChromeDriverManager().install()))
driver.get("https://sanctum.pl/register.html")
driver.maximize_window()
#Lokalizatory
x = "Josh123"
y= "josh123@gmail.com"

input_first_name = driver.find_element_by_xpath("/html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[2]/fieldset/div/input").send_keys(x)
input_last_name = driver.find_element_by_xpath("/html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[1]/fieldset/div/input").send_keys(y)

我试图把一些数据到浏览表单,但它是不工作的任何建议?我也得到这个在终端

DevTools listening on ws://127.0.0.1:50932/devtools/browser/f9638d1b-c140-4146-9d53-f5534ea6c850       
Traceback (most recent call last):
  File "e:\Python\Selenium\skrypt.py", line 19, in <module>
    input_first_name = driver.find_element_by_xpath("/html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[2]/fieldset/div/input").send_keys(x)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
PS E:\Python\Selenium> [6268:18388:0306/204146.997:ERROR:device_event_log_impl.cc(218)] [20:41:46.997] 
USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: Urz╣dzenie do│╣czonennection: Urz╣dzenie do│╣czone do komputera nie dzia│a. (0x1F)
swvgeqrz

swvgeqrz1#

此错误消息...

AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

...意味着您正在使用的 Selenium 客户端版本没有find_element_by_* commands are deprecated的 * find_element_by_xpath * 属性
溶液
取代:

driver.find_element_by_xpath("xpath_value")

您必须用途:

driver.find_elements(By.XPATH, "xpath_value")

此用例

您的有效代码行将是:

x = "Josh123"
y= "josh123@gmail.com"
input_first_name = driver.find_elements(By.XPATH, "//html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[2]/fieldset/div/input").send_keys(x)
input_last_name = driver.find_elements(By.XPATH, "//html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[1]/fieldset/div/input").send_keys(y)

时间;日期

AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

rur96b6h

rur96b6h2#

input_first_name = driver.find_element_by_xpath("/html...")

不是Python中Selenium的有效语法。
它应该是:

from selenium.webdriver.common.by import By

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options = options, service = Service(ChromeDriverManager().install()))
driver.get("https://sanctum.pl/register.html")
driver.maximize_window()
#Lokalizatory
x = "Josh123"
y= "josh123@gmail.com"

input_first_name = driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[2]/fieldset/div/input").send_keys(x)
input_last_name = driver.find_element(By.XPATH "/html/body/div[1]/div[1]/section[2]/div/div/div[2]/div[2]/div/div/div[2]/div/span/form/div[1]/fieldset/div/input").send_keys(y)

reference

相关问题