英文:
Selenium does not put data
问题
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)
<details>
<summary>英文:</summary>
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)
I was trying to put some data into browse forms but it is not working any suggestions?
I also get this in terminal
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)
</details>
# 答案1
**得分**: 0
这是您要翻译的代码部分:
```python
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()
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)
希望这对您有帮助。
英文:
input_first_name = driver.find_element_by_xpath("/html...")
Isn't a valid syntax for Selenium with Python.
It should be:
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)
答案2
得分: 0
AttributeError: 'WebDriver' 对象没有 'find_element_by_xpath' 属性。
这意味着您正在使用的 Selenium 客户端版本没有 'find_element_by_xpath' 属性,因为 'find_element_by_*' 命令已被弃用。
解决方案:
不要使用:
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' 对象没有 'find_element_by_xpath'。
英文:
This error message...
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
...implies that the version of Selenium client you are using has no attribute as find_element_by_xpath
as find_element_by_*
commands are deprecated
Solution
Instead of:
driver.find_element_by_xpath("xpath_value")
you have to use:
driver.find_elements(By.XPATH, "xpath_value")
This usecase
Your effective line of code will be:
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)
tl; dr
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论