I have python 3.10, selenium 4.3.0 and i would like to click on the following element:
<a href="#" onclick="fireLoginOrRegisterModalRequest('sign_in');ga('send', 'event', 'main_navigation', 'login', '1st_level');"> Mein Konto </a>
Unfortunately,
driver.find_element(By.CSS_SELECTOR,"a[onclick^='fireLoginOrRegisterModalRequest'][onclick*='login']").click()
does not work. See the following error message. The other opportunities wont work as well and i get a simular error message..
"C:\Users\...\AppData\Local\Programs\Python\Python310\python.exe" "C:/Users/.../PycharmProjects/bot/main.py"
C:\Users\...\PycharmProjects\bot\main.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome('./chromedriver.exe')
Traceback (most recent call last):
File "C:\Users\...\PycharmProjects\bot\main.py", line 15, in <module>
mkButton = driver.find_element(By.CSS_SELECTOR, "a[onclick^='fireLoginOrRegisterModalRequest'][onclick*='login']").click()
File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 88, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 396, in _execute
return self._parent.execute(command, params)
File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=103.0.5060.114)
Stacktrace:
Backtrace:
Ordinal0 [0x00D35FD3 2187219]
Ordinal0 [0x00CCE6D1 1763025]
Ordinal0 [0x00BE3D40 802112]
Ordinal0 [0x00C12C03 994307]
Ordinal0 [0x00C089B3 952755]
Ordinal0 [0x00C2CB8C 1100684]
Ordinal0 [0x00C08394 951188]
Ordinal0 [0x00C2CDA4 1101220]
Ordinal0 [0x00C3CFC2 1167298]
Ordinal0 [0x00C2C9A6 1100198]
Ordinal0 [0x00C06F80 946048]
Ordinal0 [0x00C07E76 949878]
GetHandleVerifier [0x00FD90C2 2721218]
GetHandleVerifier [0x00FCAAF0 2662384]
GetHandleVerifier [0x00DC137A 526458]
GetHandleVerifier [0x00DC0416 522518]
Ordinal0 [0x00CD4EAB 1789611]
Ordinal0 [0x00CD97A8 1808296]
Ordinal0 [0x00CD9895 1808533]
Ordinal0 [0x00CE26C1 1844929]
BaseThreadInitThunk [0x7688FA29 25]
RtlGetAppContainerNamedObjectPath [0x77D57A9E 286]
RtlGetAppContainerNamedObjectPath [0x77D57A6E 238]
Process finished with exit code 1
CodePudding user response:
first add id
attribute for <a>
tag
id="link"
then use the following:
# import webdriver
from selenium import webdriver
# create webdriver object
driver = webdriver.Firefox()
# get geeksforgeeks.org
driver.get("https://your.website.com")
# get element
element = driver.find_element_by_id("link")
# click the element
element.click()
or just by the text
element = driver.find_element_by_link_text("Mein Konto")
CodePudding user response:
To click on the element with text as Mein Konto you can use either of the following locator strategies:
Using partial_link_text:
driver.find_element(By.PARTIAL_LINK_TEXT, "Mein Konto").click()
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "a[onclick^='fireLoginOrRegisterModalRequest'][onclick*='login']").click()
Using xpath:
driver.find_element(By.XPATH, "//a[starts-with(@onclick, 'fireLoginOrRegisterModalRequest') and contains(., 'Mein Konto')]").click()
Ideally to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mein Konto"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='fireLoginOrRegisterModalRequest'][onclick*='login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'fireLoginOrRegisterModalRequest') and contains(., 'Mein Konto')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
CodePudding user response:
Use the following xpath -
//a[contains(@onclick,'fireLoginOrRegisterModalRequest('sign_in')')][contains(text(),'Mein Konto')]
The code will look like -
element = driver.find_element_by_xpath("//a[contains(@onclick,'fireLoginOrRegisterModalRequest('sign_in')')][contains(text(),'Mein Konto')]")
Let me know if this works.