Selenium不放置数据

huangapple go评论121阅读模式
英文:

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(&quot;detach&quot;, True)
    
    driver = webdriver.Chrome(options = options, service = Service(ChromeDriverManager().install()))
    driver.get(&quot;https://sanctum.pl/register.html&quot;)
    driver.maximize_window()
    #Lokalizatory
    x = &quot;Josh123&quot;
    y= &quot;josh123@gmail.com&quot;
    
    input_first_name = driver.find_element_by_xpath(&quot;/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&quot;).send_keys(x)
    input_last_name = driver.find_element_by_xpath(&quot;/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&quot;).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(&quot;/html...&quot;)

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(&quot;detach&quot;, True)

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

input_first_name = driver.find_element(By.XPATH, &quot;/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&quot;).send_keys(x)
input_last_name = driver.find_element(By.XPATH &quot;/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&quot;).send_keys(y)

reference

答案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: &#39;WebDriver&#39; object has no attribute &#39;find_element_by_xpath&#39;

...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(&quot;xpath_value&quot;)

you have to use:

driver.find_elements(By.XPATH, &quot;xpath_value&quot;)

This usecase

Your effective line of code will be:

x = &quot;Josh123&quot;
y= &quot;josh123@gmail.com&quot;
input_first_name = driver.find_elements(By.XPATH, &quot;//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&quot;).send_keys(x)
input_last_name = driver.find_elements(By.XPATH, &quot;//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&quot;).send_keys(y)

tl; dr

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

huangapple
  • 本文由 发表于 2023年3月7日 03:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定