Tyndale
Tyndale

Reputation: 1

Locating elements on selenium using python

I'm pretty much new with selenium and try to locate things on a page. But since I'm using a web builder, I can't really assign the elements IDs, name, and class.

I want to locate the "a", so I can element.click() it to move to another page. This is my sample code

<a class="anvil-inlinable anvil-container column-panel align-left anvil-spacing-above-small anvil-spacing-below-small anvil-component-icon-present left-icon has-text col-padding-medium anvil-component" ontouchstart="" href="javascript:void(0)" rel="noopener noreferrer" style="text-align: left;">
  <i class="anvil-component-icon left fa fa-user left-icon"></i>
  <div class="link-text" style="display: inline-block;">Sign In</div>
  <i class="anvil-component-icon right fa fa-user left-icon"></i>
</a>

I already try using find_element_by_class_name(), find_element_by_xpath(), find_element_by_css_selector() also tried using By: find_element(By.CLASS_NAME), find_element(By.XPATH)

but the result shows similar error like this

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"anvil-inlinable anvil-container column-panel align-left anvil-spacing-above-small anvil-spacing-below-small anvil-component-icon-present left-icon has-text col-padding-medium anvil-component"}

Upvotes: 0

Views: 41

Answers (1)

Ryan
Ryan

Reputation: 11

Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

It's caused by the element maybe:

  • Hidden
  • Not present on the page
  • Overlap by another element (popup, loading circle,...)

So, you should wait until the element is present or clickable before doing the next step. Take a look at the Waits documentation, I used FluentWait:

driver = Firefox()
driver.get("http://somedomain/url_that_delays_loading")
wait = WebDriverWait(driver, timeout=10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div")))

Upvotes: 1

Related Questions