I am currently working on a program that logs into an account for me and navigates through some webpages
My current issue is that on one of the webpages, I cannot click ANY hyperlink buttons to get to the next page I need to get to. I have tried finding the element waiting to see if it is clickable and using:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, ""))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, ""))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, ""))).click()
and
driver.find_element(By.XPATH, "").click()
(Along with the other methods)
However, I have had no success when I invoke a .click()
after each of those.
After further investigation, I saw that the website uses the same class name for 2 of the elements. I am not sure if this plays any role into not being able to click on the one I need to.
- Element which I am trying to click:
<a href="/prod/twbkwbis.P_GenMenu?name=bmenu.P_StuMainMnu" onm ouseover="window.status='Student'; return true" onm ouseout="window.status=''; return true" onfocus="window.status='Student'; return true" onblur="window.status=''; return true">Student</a>
The XPATH: /html/body/div[4]/table[1]/tbody/tr[2]/td[2]/a
- Element with the same class name as the one above:
<a href="/prod/twbksite.P_DispSiteMap?menu_name_in=bmenu.P_MainMnu&depth_in=2&columns_in=3" accesskey="2" >SITE MAP</a>
The XPATH: /html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]
After seeing them both having the same class name, I tried:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.submenulinktext2"))).click()
A thought I was that since the automation is going from one website URL and navigates to another page with a new URL to begin more navigation, that it did not know what URL we were on so I tried:
driver.get("NEW URL")
And with that in place there has been no success so far, unfortunately. Any guidance on how I can go about this would be appreciated!
CodePudding user response:
Considering the HTML of the element:
<a href="/prod/twbksite.P_DispSiteMap?menu_name_in=bmenu.P_MainMnu&depth_in=2&columns_in=3" accesskey="2" >SITE MAP</a>
The element is a <a>
tag with innertext as Student.
Solution
However the desired element is a dynamic element. So 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 LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Student"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onmouseover*='Student'][onfocus*='Student']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@onmouseover, 'Student') and contains(@onfocus, 'Student')][text()='Student']"))).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:
You should read this : https://selenium-python.readthedocs.io/locating-elements.html#locating-hyperlinks-by-link-text
I had the same probleme and that work for me.