Message: no such element: Unable to locate element on Selenium - python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from time import sleep
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="./chromedriver.exe")
driver.implicitly_wait(.5)
#launch URL
driver.get("https://www.b3.com.br/pt_br/market-data-e-indices/indices/indices-de-segmentos-e-setoriais/indice-imobiliario-imob-estatisticas-historicas.htm")
sleep(5)
sel = Select(driver.find_element(By.ID, 'selectYear'))
#driver.close()
driver.quit()
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="selectYear"]"}
Hello, I'm having this issue trying to simply select a dropdown element from this page. I've tried many methods already and nothing has worked so far. Is anyone able to replicate what I'm trying to do?

So the problem is that the table is inside an Iframe, you need to switch to iframe first and then perform your regular actions.
which would look something like this
sleep(5)
driver.switch_to.frame('bvmf_iframe')
sel = Select(driver.find_element(By.ID, 'selectYear'))
You may want to increase the sleep timer based on how fast/slow your webpage will load.

Related

Selenium: I'm using a working xpath which doesn't work in my project

I am a student, and as part of a lab, I'm learning Selenium, where I'm trying to use xpath to find an element on a page
For a test, I decided to take this google docs login
For this page I created a working xpath -
//div[#class="CxRgyd"]//input[#type="email"]
But my code is throwing an exception -
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class="CxRgyd"]//input[#type="email"]"}
(Session info: chrome=108.0.5359.99)
My code:
import time
import selenium as sl
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = sl.webdriver.Chrome()
driver.get('https://www.google.com/docs/about/')
driver.find_element(By.CSS_SELECTOR,
"#hero-cta-wrapper > a.glue-button.glue-button--medium-emphasis").click()
time.sleep(2)
driver.find_element(By.XPATH, '//div[#class="CxRgyd"]//input[#type="email"]').send_keys("aermilov756#gmail.com")
I will be grateful if you tell me how to make the code better, but first of all I want to understand why xpath does not work
python 3, pycharm, win10, chrome 108
What if you try first to do click into the field? I tried this and it worked:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
url = 'https://docs.google.com/document/u/0/?tgif=d&pli=1'
driver.get(url)
time.sleep(2)
email = driver.find_element(By.XPATH, '//div[#class="CxRgyd"]//input[#type="email"]')
email.click()
email.send_keys("aermilov756#gmail.com")

Selenium Python is unable to locate an element

I'm new to webdrivers and am experimenting with them.
I'm trying to click a button when opening a webpage and it keeps giving the error of unable to locate element.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("html page")
button = driver.find_element(By.ID, "onetrust-accept-btn-handler")
button.click()
i have tried id and xpath but i dont know what else to use.
the path for the button is:
/html/body/div[3]/div[3]/div/div/div[2]/div/div/button
<button id="onetrust-accept-btn-handler" tabindex="0">Run</button>
You can use implicit wait for wait until the page is fully loaded
driver.implicitly_wait(10)

Selenium: no such element: Unable to locate element:

I have a question about Selenium.
My idea:
My idea is to make a Python script that logs in to this website.
Selenium sends the username and password to the HTML input field and submits it.
Problem:
My code keeps saying:
Message: no such element: Unable to locate element:
I have tried this code with google.com for example and that works.
Why is this not working with this login page?
Can anybody help me please?
My Python code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
login_URL = ""
driver = webdriver.Chrome()
driver.get(login_URL)
time.sleep(5)
inputElement = driver.find_element_by_name('uname')
inputElement.send_keys(username)
time.sleep(20)
driver.close()
i don't know how it works in pyton, im use js
but try to use xpath
driver.find_element_by_xpath('your xpath') [maybe use 1 more click) ('xpath element').click() - element is active.
and after use send keys
******.send_keys('username')
driver.switchTo().frame(driver.findElement(By.xpath('xpath frame'))) - in js
As already explained, the element is in an iframe. Need to switch to frame to interact with the element.
It would be better apply Explicit waits.
# Imports required:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://nl.infothek-sptk.com/isps/infothek/?1043")
wait = WebDriverWait(driver,30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"body_frame")))
wait.until(EC.element_to_be_clickable((By.NAME,"uname"))).send_keys("username#mail.com")
# Code to enter other fields.
# Switch back to default to interact with elements outside the iframe.
driver.switch_to.default_content()

Having trouble clicking an HTML element with Selenium

Here's the Xpath for the button I'd like to click:
//*[#id="interstitial_join_btn"]
but when I run something like:
driver.find_element_by_xpath('//*[#id="interstitial_join_btn"]')
the console spits out:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="interstitial_join_btn"]"}
(Session info: chrome=80.0.3987.163)
This is to click the join button for a meeting within the web-version of WebEx.
If I could brute force it with pyautogui like some other stuff in my script I would but I've been scratching my head at this for days now (newbie to selenium/HTML)
Thanks
are you sure that the page is completely loaded before you trying to access the element? Maybe you have to wait a bit.
e.g.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'interstitial_join_btn')))
see also Selenium - wait until element is present, visible and interactable
and https://selenium-python.readthedocs.io/waits.html
BUT: if your HTML code you have provided is correct, you simply have a typo:
interstitial_start_btn
vs
interstitial_join_btn

Unable to locate element in Python Selenium

I'm trying to locate an element using python selenium, and have the following code:
zframe = driver.find_element_by_xpath("/html/frameset/frameset/frame[5]")
driver.switch_to.frame(zframe)
findByXpath("/html/body/form/table/tbody/tr/td[2]/label[3]").click()
element = driver.find_element_by_xpath("//*[#id='awdType']")
I'm getting the error that:
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#id='awdType']"} (Session info:
chrome=59.0.3071.115)
Any ideas why it may not be able to locate this element? I used the exact xpath by copying it and also switched frames. Thanks!
The problem occurs because awdType is loaded by ajax or jquery.
You should use selenium Waits. There is two type of waits explicit and implicit.Avoid using implicit wait.
# Explicit wait example
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver,20)
element = wait.until(EC.element_to_be_clickable((By.ID, 'awdType')))
OR
# implicit wait example
driver.implicitly_wait(10) # seconds
element = driver.find_element_by_xpath("//*[#id='awdType']")

Categories

Resources