0

I am trying to do something i can't find any help on. I want to be able to locate the xpath or other 'address' information in of a particular element for later use by selenium. I have text for the element and can find it using the selenium By.LINK.TEXT methodology. However, i am writing an application where speed is critical so i want to pre-find the element, store the xpath (for later use) and then use the By.XPATH methodology. In general finding an element using the BY.text construction takes .5 seconds whereas the xpath lookup takes on 10 - 20% of that time. I tried the code below but i get an error on getpath (WebElement object has no attribute getpath)

Thanks for any help

temp = br.find_element(By.LINK_TEXT, (str(day_to_book)))

print(temp.getpath())
Akzy
  • 1,817
  • 1
  • 7
  • 19
steve
  • 77
  • 1
  • 8

1 Answers1

0
  1. The Selenium WebElement object received by driver.find_element(ByLocator) is already a reference to the actual physical web element on the page. In other words, the WebElement object is an address of the actual web element you asking about.
  2. There is no way to get a By locator of an already found WebElement

So, in your particular example temp = br.find_element(By.LINK_TEXT, (str(day_to_book))) the temp is an address of the element you can keep for future use (until the page is changed / refreshed)

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • 1. So if the page changes and eventually comes back to this page the address retrieved above no longer works? and (2) How would i use that address in selenium to open the page? What find_element_by search would i use? Thanks SO VERY MUCH! – steve Nov 29 '22 at 20:02
  • Right. This causes Stale element reference error. See here for more explanations: https://developer.mozilla.org/en-US/docs/Web/WebDriver/Errors/StaleElementReference – Prophet Nov 29 '22 at 20:04
  • the second question is not clear – Prophet Nov 29 '22 at 20:05
  • To Prophet (nice handle!): Thanks so much for the tender treatment and referrals. I'll figure something out from here. – steve Nov 29 '22 at 21:28