1
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)
  • What line is throwing that error? My guess is that there's something in your setup/installs that isn't correct. – JeffC Mar 06 '23 at 19:53
  • Your code looks fine but your XPaths are really, really brittle. Using Copy XPath from the browser typically generates long XPaths like this. Using XPaths that start at `/html`, have a lot of levels, or have a lot of indices is generally a bad practice. I would give you a new XPath but I can't access the site. If you will put the relevant HTML for the first/last name fields, I'll give you a new locator. – JeffC Mar 06 '23 at 19:53
  • 1
    Please edit your question and post the full error message as text, properly formatted. – JeffC Mar 06 '23 at 19:57
  • I tried also shorter xpath for login //*[@id="__BVID__126"] and email //*[@id="__BVID__149"] but it didn't help, also tried on other pages and still data is not pushed – Mateusz Plsz Mar 06 '23 at 19:57
  • Dublicate? https://stackoverflow.com/a/65497385/20443541 – kaliiiiiiiii Mar 06 '23 at 19:57
  • @kaliiiiiiiii I saw this topic but this error isn't my main problem, as said there you can just ignore it and your data should be pushed - but in my case it is not – Mateusz Plsz Mar 06 '23 at 20:00

2 Answers2

0
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)

reference

kaliiiiiiiii
  • 925
  • 1
  • 2
  • 21
0

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'

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352