Python selenium unable to locate element in iframe - python

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.

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.

python selenium: element click intercepted:

I'm trying to click via find_element_by_xpath() as i did it always. In the case below, there is an error when try to click this element. I'm quite new with selenium and researched already. It seems that i have to click on specific coordination. Do you have any idea how to solve this problem? i'm struggling for quite a while.
section of HTML code:
<map ng-if="!enableSvgHighlighting" id="clickareasVillage" name="clickareasVillage"
ng-show="areas.village.length > 0" class="">
<area ng-repeat="a in areas.village" location-id="32" on-pointer-over="highlightStart(32)" on-
pointer-out="highlightStop(32)" clickable="openBuildingDialog(32)" tg-
coords="758,341,758,342,761,343,763,344,767,345,769"
coords="758,341,758,342,761,343,763,344,767,345,769"
shape="poly" building-positioner="32" class="clickable">
my Code:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
ele1=driver.find_element_by_xpath("//map[#id='clickareasVillage']/area[2]")
ele1.click()
error message:
ElementClickInterceptedException: element click intercepted: Element
<area ng-repeat="a in areas.village" location-id="32"
on-pointer-over="highlightStart(32)" on-pointer-out="highlightStop(32)" clickable="openBuildingDialog(32)"
tg-coords="758,341,758,342,761,343,763,344,767,345,769" coords="758,341,758,342,761,343,763,344,767,345,769"
shape="poly" building-positioner="32"
class="clickable"> is not clickable at point (391, 477).
Other element would receive the click: <img ng-if="!enableSvgHighlighting"
ng-show="areas.village.length > 0" class="clickareas" src="layout/images/x.gif" usemap="#clickareasVillage" data-cmp-info="9">
on other treads at stackoverflow, people suggest to use the following line. If i try, there is a TimeoutExeption.
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
As said above, usually when you encounter this error it's because a pop-up or an invisible overlay is preventing the driver from clicking on your element.
You said that JS clicking wasn't working either so i'm going to give you 2 quick workarounds that you can try:
ActionChains to move and click on your element:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).click().perform()
Send keys using ActionChains
from selenium.webdriver.common.action_chains import ActionChains
for i in range(#determine how much time):
ActionChains(driver).send_keys(Keys.TAB).perform() #tab until element is selected
ActionChains(driver).send_keys(Keys.ENTER).perform() #press enter to "click" on it
Let me know if that helped you.
Try using javascript
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
driver.execute_script("arguments[0].click();", element)
There may be something which could be preventing click on the element, check if any pop-up occuring.

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 Finding element by XPath

For reference visit https://rabirius.me/2020/02/14/bird-watching/ (not my website)
You may see a Like button there near a reblog button
I want python to click that but I get an error stating
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div[2]/a"}
The code i wrote was
for posts in open_links:
bot.get(posts)
sleep(4)
bot.find_element_by_xpath('/html/body/div/div/div[2]/a').click()
sleep(2)
The HTML of the like button is
<div class="wpl-button like">
<a href="#" title="177 bloggers like this." class="like sd-button" rel="nofollow">
<span>Like</span>
</a>
</div>
Any Help would be Appreciated
An iframe is present on the page, so you need to first switch to that iframe and then operate on the element and its recommended to use explicit wait to wait for the element to be present on the page.
You can do it like:
for posts in open_links:
bot.get(posts)
driver.switch_to.frame(bot.find_element_by_tag_name('iframe'))
like_element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'wpl-likebox')]//span[text()='Like']")))
like_element.click()
You need 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

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python

I am currently working on a project which fills a form automatically. And the next button appears when the form is filled, that's why it gives me an error.
I have tried:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//input[#type='button' and #class='button']")))
Next = driver.find_element_by_xpath("//input[#type='button' and #class='button']")
Next.click()
HTML:
<span class="btn">
<input type="button" value="Next" class="button" payoneer="Button" data-controltovalidate="PersonalDetails" data-onfieldsvalidation="ToggleNextButton" data-onclick="UpdateServerWithCurrentSection();" id="PersonalDetailsButton">
</input>
<div class="clearfix"></div>
</span>
ERROR:
selenium.common.exceptions.ElementClickInterceptedException: Message:
element click intercepted: Element is not clickable at point (203, 530).
Other element would receive the click: ... (Session info: chrome=76.0.3809.132)
If the path of the xpath is right, maybe you can try this method to solve this problem. Replace the old code with the following code:
button = driver.find_element_by_xpath("xpath")
driver.execute_script("arguments[0].click();", button)
I solved this problem before, but to be honestly, I don't know the reason.
This error message...
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (203, 530). Other element would receive the click: ... (Session info: chrome=76.0.3809.132)
...implies that the click() on the desired element was intercepted by some other element and the desired element wasn't clickable.
There are a couple of things which you need to consider as follows:
While using Selenium for automation using time.sleep(secs) without any specific condition to achieve defeats the purpose of automation and should be avoided at any cost. As per the documentation:
time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
You can find a detailed discussion in How to sleep webdriver in python for milliseconds
As WebDriverWait returns the WebElement you can invoke the click() method directly.
Solution
To click on the button with value as Next you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#PersonalDetailsButton[data-controltovalidate='PersonalDetails']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button' and #id='PersonalDetailsButton'][#data-controltovalidate='PersonalDetails']"))).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
i faced similar issues, the .click() always returns a Not clickable exception. the
driver.execute_script('arguments[0].click()', button)
does the magic. You can also use it to execute any other js script this way
script = 'your JavaScript goes here'
element = driver.find_element_by_*('your element identifier goes here')
driver.execute_script(script, element)
I looked at the exact element that was causing it and it was a banner about consent/cookies. So at first, I made sure it clicked "OK" on the consent banner and then I clicked the other button that I needed. Hope it helps someone.
It look's like there are some other elements which are having the same xpath try changing the xpath something like this
Next = driver.find_element_by_xpath("//input[#id='PersonalDetailsButton']");
Next.Click();
or
Next = driver.find_element_by_xpath(//input[#value='Next' and #id='PersonalDetailsButton']);
Next.Click();
Try first xpath if that doesn't work go with the second one . If that also doesn't work try using sikuli. I am pretty sure that first xpath will work
I faced a similar issue and I observed something that might help to understand the root cause of the issue. In my case, I was able to click at an element being in PC view mode of the website but failed to do so in mobile view (in which I needed my script to run). I found out that in mobile view, ordering of elements (li in my case) changed in view while they remained same in the html document. That's why I was not able to click on it without actually scrolling to it first. It might also explain why this works: -
driver.execute_script("arguments[0].click();", button)
I don't have enough rep to comment but the common reason for this error might be Selenium locates the element from DOM on screen and locate the x-y coordinates (300, 650) then clicks on them but if some changes takes place on screen in between the click duration, for example google ads or some pop-up then it's unable to click on it resulting in this exception
I'm just guessing if anyone has a proper explanation to pls share
I had the same problem too. But my problem was not with the element. The button was activated with href. I changed the code from
<a class="services-button" href="desired url">
To
<a class="services-button" onclick="location.href='{% url "desired url" %}'";">
This solution worked for me :
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox(executable_path="")
driver.get("https://UrlToOpen")
action = ActionChains(driver)
firstLevelMenu = driver.find_element_by_id("menu")
firstLevelMenu.click()
source : http://allselenium.info/mouse-over-actions-using-python-selenium-webdriver/
"selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable ... "
This exception occurs when element is not found on a web page (When the element we are looking for is at bottom part of the page which is not loaded yet)
So we you can scroll the page using javascript and load complete page and
from selenium.webdriver.common.by import By
from selenium import webdriver
url = "YOUR URL"
SCROLL_PAUSE_TIME = 0.5
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
def scroll_page():
i = 0
while i < 5:
# Scroll down to 500 pixel
driver.execute_script("window.scrollBy(0, 500)", "")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Will scroll only for 4 increments of 500px
i += 1
You could try:
driver.execute_script("arguments[0].click();", button)
This solution solved my problems when I faced similar issues.

Categories

Resources