Issue
I'm trying use selenium and firefox, but it's just open the browser and set this error:
With Chrome is ok.
This is the code:
from selenium import webdriver
driver = webdriver.Firefox(executable_path = "c:/my-apps/geckodriver.exe")
Thanks very much
Solution
When you try to find any element in an HTML
page that does not exist, NoSuchElementException
will be raised.
First Possible Solution:
You need to import
from selenium.common.exceptions import NoSuchElementException
Then you can use try except
block
try:
your_element = driver.find_element_by_xpath(".//*[@id='loginForm:username']")
your_element.click()
except NoSuchElementException:
pass
Second Possible Solution:
Without importing anything, checking whether that element exist, if it does then it will be clicked
your_element = driver.find_elements_by_xpath(".//*[@id='loginForm:username']")
if len(your_element) > 0:
elem[0].click()
Answered By - Fareed Khan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.