Having trouble clicking an HTML element with Selenium - python

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

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.

Button element not interactable Selenium

I'm trying to write a script to automate some tasks with Selenium and Python, and every time I try to click on a button
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome(options=options)
xpath = "/html/body/app-root/app-prime/div/mat-sidenav-container//app-detail-component/main//div/span/button[#aria-label='Prenota']"
# Wait for the element to be visible, always true
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, xpath)))
# Try to click on element, get an error
driver.find_element(By.XPATH, xpath).click()
I get the following error
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I know for sure that the element gets located correctly and has to match, but it should act only on the first one.
I tried:
Trying to click child tags, such as others divs and span
Waiting for it to be clickable
Waiting with an implicit wait
None of those activities were successful
Edit: Apparently the issue olly exist on my machine
It could be not clickable for a number of reasons. You might want to check if there is some element on the page layered on top so that that element is not interactable at that time, e.g some popup/iframe etc. There could be some other element that will receive click at that time.
You could try an actions click - something like this
myElement = driver.find_element_by_xpath("myXpath")
webdriver.ActionChains(driver).move_to_element(myElement).click(myElement).perform()
One of this should work.
IJavaScriptExecutor executor = (IJavaScriptExecutor)WebDriver.Driver;
executor.ExecuteScript("arguments[0].click();", webElement);
Actions actions = new Actions(WebDriver.Driver);
actions.MoveToElement(webElement).Click().Perform();
Note - these are c sharp code. try to do the same in java.

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.

Selenium can't locate the element, a search button using xpath

I can't find a solution how this element cannot be found by using a selenium xpath. Other button on other websites always working just fine. Usually what I would normally do is, just open up a page and inspect the element and I would right click it and copy the xpath. Done.
But this website, www.gsc.com.my (a malaysian cinema booking site). Seems not able to find the button. Is it protected by another security layer?
Lets see the code below,
from selenium import webdriver
chromedriver_path = './chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)
driver.get('https://www.gsc.com.my')
driver.find_element_by_xpath("""//*[#id="btnNext"]""").click()
The error Message:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="btnNext"]"}
Button is located inside an iframe, so you need to switch to that frame before clicking the button:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver.switch_to.frame('getQuickFilter_ctrl_ifrBox')
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()
Because there are two elements with id btnNext, you'll have to specify which of them using an index, 1 for the first, 2 for the second.
driver.find_element_by_xpath("""//*[#id="btnNext"][1]""").click()
You can try with this css selector :
div.container input#btnNext
Note that you will need to switch to iframe first , cause the check button is in a iframe.
For switching to iframe :
driver.switch_to.frame(driver.find_element_by_id("getQuickFilter_ctrl_ifrBox"))
and for clicking on check Button
wait = WebDriverWait(driver, 10)
check_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.container input#btnNext")))
check_button.click()

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