This is the page I am trying to scrape: https://directory.brcgs.com/site/10005068
My code:
bk_btn = driver.find_element_by_xpath("//button [#class='BackButton_backButton__3Czsm']")
bk_btn.click()
This is the error I receive:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button [#class='BackButton_backButton__3Czsm']"}
Why am I getting this error and how can I correct it?
Your locator is correct.
It's quite clear that you are missing a delay.
You need to make the element completely loaded before clicking it.
The preferred way to do that is to use WebDriverWait explicit waits as following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='BackButton_backButton__3Czsm']"))).click()
Related
With selenium-python I want to select an iframe which has the following information:
<iframe title="Key to fetch simulation results" class="ltiLaunchFrame" name="ltiFrame-1548f8973dbf4d76840c56763e996767" src="/courses/course-v1:EPFL+SimNeuro2+2019_2/xblock/block-v1:EPFL+SimNeuro2+2019_2+type#lti_consumer+block#1548f8973dbf4d76840c56763e996767/handler/lti_launch_handler" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" allow="microphone *; camera *; midi *; geolocation *; encrypted-media *"></iframe>
I tried to do
webdriver.switch_to.frame("ltiFrame-1548f8973dbf4d76840c56763e996767")'
but it says
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="ltiFrame-1548f8973dbf4d76840c56763e996767"]
But I have used the correct name! What am I doing wrong?
In case title attribute value Key to fetch simulation results is unique this should work:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Key to fetch simulation results']")))
In order to use this you have to import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
and initialize wait object
wait = WebDriverWait(driver, 20)
//iframe[contains(#name, 'ltiFrame-') and #class='ltiLaunchFrame']
you can use the above mentioned xpath to locate the iframe.
Code :
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(#name, 'ltiFrame-') and #class='ltiLaunchFrame']")))
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
also in case you wanna come out of the frame, use the below code :
driver.switch_to.default_content()
button = self.driver.find_element_by_xpath(
"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button")
button.click()
when I try to run my code I get the exception: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button"}
(Session info: chrome=91.0.4472.164
meanwhile its the xpath of the button which is found https://www.zalando.fr/login
and this is the element
Se connecter
i've tried finding it by class name and everything it just doesn't work
Your locator is wrong.
Also use explicit wait.
Try this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-testid='login_button']"))).click()
you can try this where need to handle the cookie window which is getting displayed after the page is getting loaded and used explicitWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
driver.get("https://www.zalando.fr/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='OK']"))).click()
#here I'm passing the user name
driver.find_element_by_xpath("//*[#id='login.email']").send_keys("email")
#here passing password
driver.find_element_by_xpath("//*[#id='login.password']").send_keys("password")
#clicking on the `Se connecter` button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-testid='login_button']"))).click()
The xpath ended up being wrong. I copied it from chrometools and tried later with brave and I got the correct xpath.
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()
I have a program which uses the python selenium webdriver and I get the following runtime error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='id_login']"}
(Session info: chrome=83.0.4103.1
HTML
<input type="text" name="login" placeholder="Type your username" required="" id="id_login" xpath="1">16)
code:
from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="c:\\Chrome1\chromedriver.exe")
driver.get("https://www.jstor.org")
print(driver.title)
driver.find_element_by_xpath("//a[#class='inline-block plm']").click()
driver.find_element_by_xpath("//input[#id='id_login']").send_keys('xxxxxx#gmail.com')
This error means that selenium could not localize the element because it was not on the site or it did not load. I suggest you using the WebdriverWait() function. It will wait X seconds until the element is clickable. If it will still not be, it will throw an error.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.webdriver, 60).until(EC.element_to_be_clickable((By.XPATH, '//a[#class='inline-block plm']')))
More effective if you direct directly to this url:
https://www.jstor.org/action/showLogin?redirectUri=/
And use selenium wait to solve your issue.
driver.get('https://www.jstor.org/action/showLogin?redirectUri=/')
user_name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']")))
user_name.send_keys('xxxxxx#gmail.com')
Although your xpath will work, using an id looks better .element_to_be_clickable((By.ID, "id_login"))
You need following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence to the Username field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.jstor.org/")
ebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.inline-block.plm"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id_login"))).send_keys("SunilTirupathi#stackoverflow.com")
Using XPATH:
driver.get("https://www.jstor.org/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='inline-block plm']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']"))).send_keys("SunilTirupathi#stackoverflow.com")
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
Browser Snapshot:
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
I was trying to access the search bar of this website: https://www.kissanime.ru
using selenium. I tried it using xpath, class, css_selector but every time this error pops up in the terminal.
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: //*[#id="keyword"]
My approach to the problem was :
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Firefox()
driver.get("https://kissanime.ru/")
driver.maximize_window()
search=driver.find_element_by_xpath('//*[#id="keyword"]')
search.send_keys("boruto")
search.send_keys(Keys.RETURN)
Try adding some wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
search = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, "keyword")))
Add wait to avoid race condition
driver.implicitly_wait(20)