Unable to locate element in Python Selenium - python

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']")

Related

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

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.

Selenium returns NoSuchElementException Error

I`m a newbie to python.
Recently I got interested in Web Crawling.
Today I got stuck in NoSuchElementException
This is the webpage that i want to scrape.
When I click the username that i erased, it returns box like this.
Though I used the xpath that i copied from Chrome developer tool,
it returns me NoSuchElementException:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="main-area"]/div[4]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a"}
(Session info: chrome=87.0.4280.88)
HTML is like this
이주연마인
My code is just like this,
driver.find_element_by_xpath("//*[#id=\"main-area\"]/div[4]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a")
I checked there is this xpath, but when I get it into .find_element_by_xpath() method it returns Error.
I do really share the webpage, but it needs to log-in to get there,
So i cannot share the webpage.
Could you guess what might cause this problem?
I checked time is not the problem.
I checked iframe is not the problem.
Thank you in advance
Have a great day!
To locate the element with text as 이주연마인 you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using LINK_TEXT:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "이주연마인")))
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.m-tcol-c[onclick*='royaltina']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='m-tcol-c' and contains(#onclick, 'royaltina')][text()='이주연마인']")))
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

Python selenium unable to locate element in iframe

I am very confused about why I cannot select any element under this iframe.
The HTML is like this:
html code
I see two iframes here, with one inside the other.
I need to go to "switcher_plogin" under the inner iframe.
The relevant html code is:
<a class="link" hidefocus="true" id="switcher_plogin" href="javascript:void(0);" tabindex="8">text here</a> == $0
And here is my python code:
driver.switch_to.frame(1) # to switch to the second iframe
driver.find_element_by_id('switcher_plogin').click()
But the error says:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".bottom hide"}
Please someone help me.
Thanks a lot.
You cannot switch to child frame directly.
Switch to second frame like below code:
driver.switch_to.frame("qqAuthFrame_8639242")
driver.switch_to.frame("ptlogin_iframe")
Or try to use WebDriverWait to wait frames be available to switch.
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "qqAuthFrame_8639242")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "ptlogin_iframe")))
# This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds.

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

Python/Splinter + Selenium error: Element is not clickable at point (657, 724) [duplicate]

This question already has answers here:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
(9 answers)
Closed 5 years ago.
While trying out web scraping at https://store.obeygiant.com/collections/prints/products/obey-ripped-signed-offset-poster, made an attempt to click a button by id and even by name:
browser.find_by_id('AddToCartText').click()
browser.find_by_name('add').click()
But got the following error for either attempt:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (657, 724)
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.11.6 x86_64)
Tested it out the same way and other sites actually worked.
What could be the issue and what would be a good approach to it?
Thank you in advance and will be sure to vote up and accept answer.
This may be happened where after get() elements were not instantly available to click() so to avoid such issues you can use webdriver wait which can check if elements are present and clickable or not after that you can perform click() or any respective operation.
You can use webdriverwait refer.
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.Chrome("C:\\temp\\chromedriver.exe")
url = r"https://store.obeygiant.com/collections/prints/products/obey-ripped-signed-offset-poster"
driver.get(url)
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "AddToCartText"))) # to check presence of element
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'AddToCartText'))) # to check if element is clickable or not
element.click() # after that performs click()
driver.close()
Same is applicable for other element only change would be locator.

Categories

Resources