Selenium不放置数据

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

Selenium does not put data

问题

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. options = webdriver.ChromeOptions()
  5. options.add_experimental_option("detach", True)
  6. driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))
  7. driver.get("https://sanctum.pl/register.html")
  8. driver.maximize_window()
  9. # Lokalizatory
  10. x = "Josh123"
  11. y = "josh123@gmail.com"
  12. 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)
  13. 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)

你正在尝试将一些数据输入到浏览器表单中,但它不起作用,有什么建议吗?

我还在终端中看到以下内容:

  1. DevTools listening on ws://127.0.0.1:50932/devtools/browser/f9638d1b-c140-4146-9d53-f5534ea6c850
  2. Traceback (most recent call last):
  3. File "e:\Python\Selenium\skrypt.py", line 19, in <module>
  4. 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)
  5. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  6. AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
  7. PS E:\Python\Selenium> [6268:18388:0306/204146.997:ERROR:device_event_log_impl.cc(218)] [20:41:46.997]
  8. USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: Urzdzenie do│╣czonennection: Urzdzenie do│╣czone do komputera nie dziaa. (0x1F)
  1. <details>
  2. <summary>英文:</summary>
  3. from selenium import webdriver
  4. from selenium.webdriver.chrome.service import Service
  5. from webdriver_manager.chrome import ChromeDriverManager
  6. options = webdriver.ChromeOptions()
  7. options.add_experimental_option(&quot;detach&quot;, True)
  8. driver = webdriver.Chrome(options = options, service = Service(ChromeDriverManager().install()))
  9. driver.get(&quot;https://sanctum.pl/register.html&quot;)
  10. driver.maximize_window()
  11. #Lokalizatory
  12. x = &quot;Josh123&quot;
  13. y= &quot;josh123@gmail.com&quot;
  14. 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)
  15. 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)
  16. I was trying to put some data into browse forms but it is not working any suggestions?
  17. 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)

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 这是您要翻译的代码部分:
  5. ```python
  6. from selenium.webdriver.common.by import By
  7. from selenium import webdriver
  8. from selenium.webdriver.chrome.service import Service
  9. from webdriver_manager.chrome import ChromeDriverManager
  10. options = webdriver.ChromeOptions()
  11. options.add_experimental_option("detach", True)
  12. driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))
  13. driver.get("https://sanctum.pl/register.html")
  14. driver.maximize_window()
  15. x = "Josh123"
  16. y = "josh123@gmail.com"
  17. 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)
  18. 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)

希望这对您有帮助。

英文:
  1. input_first_name = driver.find_element_by_xpath(&quot;/html...&quot;)

Isn't a valid syntax for Selenium with Python.

It should be:

  1. from selenium.webdriver.common.by import By
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from webdriver_manager.chrome import ChromeDriverManager
  5. options = webdriver.ChromeOptions()
  6. options.add_experimental_option(&quot;detach&quot;, True)
  7. driver = webdriver.Chrome(options = options, service = Service(ChromeDriverManager().install()))
  8. driver.get(&quot;https://sanctum.pl/register.html&quot;)
  9. driver.maximize_window()
  10. #Lokalizatory
  11. x = &quot;Josh123&quot;
  12. y= &quot;josh123@gmail.com&quot;
  13. 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)
  14. 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_*' 命令已被弃用。

解决方案:
不要使用:

  1. driver.find_element_by_xpath("xpath_value")

而是使用:

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

在您的用例中,有效的代码行将是:

  1. x = "Josh123"
  2. y = "josh123@gmail.com"
  3. 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)
  4. 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...

  1. 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:

  1. driver.find_element_by_xpath(&quot;xpath_value&quot;)

you have to use:

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

This usecase

Your effective line of code will be:

  1. x = &quot;Josh123&quot;
  2. y= &quot;josh123@gmail.com&quot;
  3. 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)
  4. 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:

确定