1

I have tried every which way and I keep getting the same error. Not sure what the issue is. Below is my code:

`import time

import selenium

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.chrome.webdriver.WebDriver(executable_path='C:/Users/mycomp/Downloads/chromedriver_win32/chromedriver.exe')
driver.get("https://www.linkedin.com")

time.sleep(5)

username = driver.find_element(By.XPATH, "// input[@name= 'session_keys'")
username.send_keys("my email")
pword = driver.find_element(By.XPATH, "// input[@name= 'session_password'")
pword.send_keys("my password")

`

I am not sure why am getting the error below. It is just a simple click. Is it that I am using the wrong version of chromedriver or something else? Here is the error I keep getting:

`C:\Users\mycomp\Desktop\venv\Scripts\python.exe C:\Users\mycomp\pythonProject2\main.py 
C:\Users\mycom\pythonProject2\main.py:8: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.chrome.webdriver.WebDriver(executable_path='C:/Users/mycomp/Downloads/chromedriver_win32/chromedriver.exe')
Traceback (most recent call last):
File "C:\Users\mycomp\pythonProject2\main.py", line 13, in <module>
username = driver.find_element(By.XPATH, "// input[@name= 'session_keys'")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mycomp\Desktop\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mycomp\Desktop\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\mycomp\Desktop\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression // input[@name= 'session_keys' because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '// input[@name= 'session_keys'' is not a valid XPath expression.
(Session info: chrome=110.0.5481.78)
Stacktrace:
Backtrace:
(No symbol) [0x00736643]
(No symbol) [0x006CBE21]
(No symbol) [0x005CDA9D]
(No symbol) [0x005D09E4]
(No symbol) [0x005D08AD]
(No symbol) [0x005D0B30]
(No symbol) [0x00600FAC]
(No symbol) [0x0060147B]
(No symbol) [0x00638DC2]
(No symbol) [0x0061FDC4]
(No symbol) [0x00636B09]
(No symbol) [0x0061FB76]
(No symbol) [0x005F49C1]
(No symbol) [0x005F5E5D]
GetHandleVerifier [0x009AA142+2497106]
GetHandleVerifier [0x009D85D3+2686691]
GetHandleVerifier [0x009DBB9C+2700460]
GetHandleVerifier [0x007E3B10+635936]
(No symbol) [0x006D4A1F]
(No symbol) [0x006DA418]
(No symbol) [0x006DA505]
(No symbol) [0x006E508B]
BaseThreadInitThunk [0x75787D69+25]
RtlInitializeExceptionChain [0x76F2BB9B+107]
RtlClearBits [0x76F2BB1F+191]


 Process finished with exit code 1

`

1 Answers1

1

As the error says,

The string '// input[@name= 'session_keys'' is not a valid XPath expression.

You need the ending ]. So change it to

username = driver.find_element(By.XPATH, "// input[@name= 'session_keys']")
pword = driver.find_element(By.XPATH, "// input[@name= 'session_password']")

Edit: After testing, the other problem is that you wrote 'session_keys' when it should be 'session_key'

Luke B
  • 2,075
  • 2
  • 18
  • 26
  • Sorry it's still not working: – user2279030 Feb 12 '23 at 00:21
  • @user2279030 in what way? Is it still giving the same error? – Luke B Feb 12 '23 at 00:25
  • Yes getting the same error:File "C:\Users\mycomp\pythonProject2\main.py", line 13, in username = driver.find_element(By.XPATH, "// input[@name= 'session_keys']") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mycomp\Desktop\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] – user2279030 Feb 12 '23 at 00:26
  • @user2279030 Are you sure it is the exact same? When I run it I get the different error "unable to locate element". I have updated my answer with a fix for that. – Luke B Feb 12 '23 at 00:34
  • It worked! awesome. However, the browser is automatically closing for some reason. I don't have a close statement at the end – user2279030 Feb 12 '23 at 00:41
  • 1
    @user2279030 There are several solutions to that problem, look at [https://stackoverflow.com/questions/51865300/python-selenium-keep-browser-open](https://stackoverflow.com/questions/51865300/python-selenium-keep-browser-open). If you have more problems with that you should open a new question. – Luke B Feb 12 '23 at 00:45