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 ontouchstart="" href="javascript:void(0)" rel="noopener noreferrer" style="text-align: left;">
<i ></i>
<div style="display: inline-block;">Sign In</div>
<i ></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"}
CodePudding user response:
If you are using Chrome browser or Firefox browser, you can use the following code to identify the element.
driver.find_element(By.CLASS_NAME, "class_name_to_be_identified").click()
If you are using IE browser, you can use the following code to identify the element.
driver.find_element(By.CLASS_NAME, "class_name_to_be_identified").send_keys("your text")
Note: If you are having multiple classes, you can use the first class which is written in tag in class attribute. Let me know if it works for you or not.
CodePudding user response:
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")))