Selenium (Python) - NoSuchElementException: Message: no such element: Unable to locate element - python

I'm trying to make a simple script that goes to the homepage of The Whiskey Exchange, clicks on the following menu item, navigates to the new page and finally grabs a screenshot.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.thewhiskyexchange.com/")
driver.implicitly_wait(3)
search_bar = driver.find_element(
By.XPATH, "/html/body/div[3]/nav/div/div[3]/div/div/div[2]/a[2]")
search_bar.click()
driver.implicitly_wait(3)
driver.save_screenshot('./image.png')
driver.quit()
I've tried multiple approaches to get this to work, mainly by changing the type of finder, but with no luck. I've tried copying both XPATH and Full XPATH (directly from chrome), but that hasn't worked either. Maybe it's something to do with how the site is built, but no idea.
I'm getting the following error message:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/nav/div/div[3]/div/div/div[2]/a[2]"}
First time using selenium, so the struggle is a bit real. Please help.

To expand the Scotch Whisky menu you don't need to click on the item, instead you can simply Mouse Hover and grab a screenshot of the webpage using either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.thewhiskyexchange.com/')
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[title='Scotch Whisky']")))).perform()
driver.save_screenshot('./Scotch_Whisky.png')
driver.quit()
Using XPATH:
driver.get('https://www.thewhiskyexchange.com/')
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#title='Scotch Whisky']")))).perform()
driver.save_screenshot('./Scotch_Whisky.png')
driver.quit()
Note : You have to add the following imports :
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Screenshot:

CSS_SELECTOR
a.header-button.header-button--search.js-header-button--search
XPATH
//a[#title='Search']
You could simply use this css selector since I don't see an xpath like that.

Related

Python selenium web-driver: selecting value from the dropdown list (No such Element Exception)

I am trying to extract the user reviews from google app using selenium webdriver. I loaded all reviews on the web-browser. Now I want to change the 'Most relevant' option of review page to 'Newest' option as shown in the picture.
Here is my code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
baseurl = 'https://play.google.com/store/apps/details?id=com.mapmyrun.android2&showAllReviews=true'
driver.get(baseurl)
driver.find_element_by_xpath("//div[#class='OA0qNb ncFHed']//div[#class='MocG8c UFSXYb LMgvRb KKjvXb']//span[#class='vRMGwf oJeWuf']").click()
When I ran the code, it throws following error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='OA0qNb ncFHed']//div[#class='MocG8c UFSXYb LMgvRb KKjvXb']//span[#class='vRMGwf oJeWuf']"}
To click on google review options as Newest Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath option.
driver.get("https://play.google.com/store/apps/details?id=com.mapmyrun.android2&showAllReviews=true")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[./span[text()='Most relevant']]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#role='option'][./span[text()='Newest']]"))).click()
Browser snapshot:
If you want to simply click the div and click newest. Your class is dynamic and will change it's better to get an indentification that won't change.
driver.get("https://play.google.com/store/apps/details?id=com.mapmyrun.android2&showAllReviews=true")
driver.find_element_by_xpath("//span[text()='Most relevant']/parent::div").click()
time.sleep(3)
driver.find_elements_by_xpath("//span[text()='Newest']")[1].click()

Python Selenium, help me locate an element in a website

I want to click on "new order" icon in mt4 web terminal using selenium module in python
This is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
new_order = driver.find_element_by_xpath('/html/body/div[3]/div[1]/a[1]/span[1]')
new_order.click()
And this is the error that I get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[1]/a[1]/span[1]"}
(Session info: chrome=86.0.4240.198)
What is the correct way to locate that button, I searched and found some ways to locate elements for selenium but I couldn't get any of them work for me.
Looks like your page is dealing with iframes. So while the above answer has good practices, you also need to switch to the iframe:
driver.switch_to.iframe(self,frame reference)
Look for more details at https://www.techbeamers.com/switch-between-iframes-selenium-python/ or https://stackoverflow.com/a/24286392/1387701
The element with tooltip as New Order is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following xpath based Locator Strategies:
driver.get('https://www.mql5.com/en/trading')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='webTerminalHost']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Connect to an Account']//following-sibling::div[1]/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='New Order']/span"))).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
You can use a different xpath:
new_order = driver.find_element_by_xpath('//a[#title="New Order"]')
But I would suggest By, WebDriverWait, and expected_conditions:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
time.sleep(5)
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//iframe[#id="webTerminalHost"]')))
driver.switch_to.frame(iframe)
new_order = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[#title="New Order"]')))
new_order.click()

Cannot find element using selenium (python)

So I have tried looking at the iframes in the website but cannot figure out where this element falls under. Im trying to access an element by class name. Here is my code below and here is the website.
from requests import get
from bs4 import BeautifulSoup
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
browser = webdriver.Chrome(executable_path= '/Users/abeelcf/Downloads/chromedriver')
browser.get('https://www.redfin.com')
zipcode = input("Enter a zip code to look up: ")
search_form = browser.find_element_by_id('search-box-input')
search_form.send_keys(zipcode)
search_form.submit()
#pg 2
browser.find_element_by_id("MapHomeCard_0")
The URL is https://www.redfin.com/zipcode/20007
The problem is with the last line. It cannot find element MapHomeCard_0 saying the element no such element.
You need to wait for the page to load after the search_box submit use a web driver wait.
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'MapHomeCard_0')))
Also import the following
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using XPATH:
driver.get('https://www.redfin.com')
search_form = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='search-input-box' and #id='search-box-input'][#title='City, Address, School, Agent, ZIP']")))
search_form.send_keys("20007")
search_form.submit()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='homecards']/div[#id='MapHomeCard_0']"))).text)
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.homecards>div#MapHomeCard_0"))).text)
Console Output:
LISTED BY REDFIN
3D WALKTHROUGH
$464,000
1 Bed
1 Bath
1,014 Sq. Ft.
2500 Q St NW #412, Washington, DC 20007
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
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How to click on the Sign in element with GitHub page https://github.com/ using Selenium and Python

I'm trying to use Selenium for python to click on the sign in link at the top of github. I've tried using find_element_by_link_text() but i get a NoSuchElementException. I then tried using find_element_by_xpath() and I got an ElementNotinteractableException. Here is the code for the first:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://github.com')
signin = browser.find_element_by_link_text('Sign in')
signin.click()
and here's the code for the second.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://github.com')
signin_link = browser.find_element_by_xpath('/html/body/div[1]/header/div/div[2]/div[2]/a[1]')
signin_link.click()
I even tried find_element_by_css_selector() but also got an ElementNotInteractableException. I don't understand what's going wrong. I don't feel like putting in the html, but if you go to github, it's just the sign in link at the very top right.
I think you are missing out to pass chromedriver path. Try this:
browser = webdriver.Chrome(r"C:\Users\...\chromedriver.exe")
Also, If you want to go to the login page, then I would recommend avoiding the long way root. What I mean is, the following code should directly take you to the login page:
browser.get('https://github.com/login')
However, if you must know, how else you can click that element, try looping over "href" elements:
for el in browser.find_elements_by_tag_name("a"):
if "/login" in el.get_attribute('href'):
el.click()
To handle dynamic element induce WebDriverWait() and wait for element_to_be_clickable() and use following locator strtegies.
LINK_TEXT:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT,"Sign in")))
signin.click()
XPATH:
browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.XPATH,"//a[#href='/login']")))
signin.click()
CSS Selector:
browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,"a[href='/login']")))
signin.click()
To click() on the Sign in element at the top right corner of GitHub page https://github.com/ using Selenium, you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sign"))).click()
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(., 'Sign')]"))).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

My selenium program fails to find element

I am trying to perform a web automatization with python and selenium in chrome.
The thing is im trying to locate a button which has not id or class name.
The xpath is:
//*[#id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]
And the html code is
<span class="SectionMethod" onclick="window.location.href="explorer/explorer.aspx?root=user";" style="cursor:pointer;text-decoration:underline;color:CadetBlue;">Open</span>
It's a button called open, but there are other buttons like that one with the same text and class so i cant locate by text.
This is my code:
from selenium import webdriver
driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
driver.get("..............")
driver.find_element_by_xpath('//*[#id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]')
This is the error i am getting:
NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Form1"}
(Session info: chrome=75.0.3770.100)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.16299 x86_64).
It's likely you are looking for an element before it has loaded. As per the example from the documentation
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
In your case:
EC.presence_of_element_located((By.ID, "myDynamicElement"))
Will be
EC.presence_of_element_located((By.XPATH, '//*[#id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]'))
If that doesn't fix the error I suggest you review how to form a MCVE, and how to ask a well received question (recommended reading when creating a new account). Then edit your question into a more concise format, so we can more effectively help you! Welcome to StackOverflow.
Your correct xpath is:
//span[#class='SectionMethod' and text() = 'Open']
Presumably you are trying to click() on the <span> element with text as Open and to achieve that you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.SectionMethod[onclick*='explorer/explorer']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='SectionMethod' and contains(#onclick,'explorer/explorer')][text()='Open']"))).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
Additionally, as you are using chrome=75.0.3770.100 you need to update ChromeDriver to ChromeDriver 75.0.3770.90 (2019-06-13)

Categories

Resources