Unable to locate element with Selenium on a website - python

i am trying to go to a certain website with selenium and click a button to load data then download csv file, I have tried using id,Xpath, class by I am still getting an error: any hint will be helpful.
here is my code below:
from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://eatradingacademy.com/software/forex-historical-data/')
driver.find_element_by_id("webpushr-deny-button").click()
driver.find_element_by_class_name('text-center')
here is the error i am getting:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".text-center"}
(Session info: chrome=91.0.4472.124)

I think the error is due to the button you're trying to click is wrapped inside an iframe.
Typically, you will have switch to an iframe in order to access elements inside it.
Try
driver.switch_to.frame(driver.find_element_by_id('data-app-frame'))
This will switch the webdriver to the iframe having id data-app-frame
Now try clicking your button using driver.find_element_by_id('btn-load-data')
This should work.
Here I am accessing the same button you tried to access in the question. In the question you are trying to access parent div instead of the button element itself. It is recommended to access the actual element instead of it's parent
You can learn more about frame switching here

Related

Can't find element with XPATH despite being found in the console search bar

I'm trying to implement a program that fills certain section of a MS One page, I've been using Python and selenium for this task, I can access the webpage ok, I can send keys in the main page, but in the client's page I can't locate the elements.
I've tried:
By.ID
By.XPATH (xpath and full xpath)
To no avail, the code I'm using for that is:
sel = driver.find_element(By.XPATH, "//*[#id='cmtx_pasaporte_i']")
sel.send_keys("198282828")
I get the error:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='cmtx_pasaporte_i']"}
(Session info: chrome=109.0.5414.75
But when I search the xpath in the console I find the element right away.
I'm new at python, webscrapping and everything, thanks in advance for any help.
console

selenium-driver :looking a solution for locating and clicking a button when i open google page

python selenium driver:
No thanks
when i open a google page the, there is a small window asking if i want to sign in or not. i want to click on "No thanks" button, which is as shown above.
i have tried these methods so far, but i keep getting errors. None of the following is working.
#self.driver.find_element(By.CSS_SELECTOR, 'button.M6CB1c')
#button=self.driver.find_elements(By.XPATH, '//button')
#abc=self.driver.find_elements(By.NAME, 'ZUkOIc').click()
#self.driver.find_element(By.TAG_NAME, 'button').click()
error message for the 1st line of code:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".M6CB1c.rr4y5c"}
selenium.common.exceptions.NoSuchElement is caused when the element is not in the page at the current time.
TLDR;
What you're looking for is Explicit Wait in selenium. You need to use WebDriverWait with expected condition element_to_be_clickable.
When we load a page, modern pages tend to load javascript that can often manipulate DOM (html page objects). The proper way to handle this is to wait for the page or the required element to load, and then try to locate it.
The selenium waits section explains this very well with an example.
You should try this :
driver.find_element(By.XPATH, '//button[#id="W0wltc"])

Unable to click on visible Search icon - Selenium Python (Geckodriver)

I am not able to click on a visible Search button (Selenium - Python, geckodriver). Tried ID, XPATH and CSS, none of the options work.
Below is the message for Xpath
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //img[#id='pageToolbar_search_image']
Anyone faced similar problems?
Script:
Searchicon=driver.find_element(By.XPATH,"//img[#id='pageToolbar_search_image']")
Searchicon.click()
Code:
There may be several reasons. Most likely you did not wait for the appearance/rendering or use the wrong locator
If you can find an element by xpath using devtools, then time.sleep() may help, use it before trying to find the element
https://selenium-python.readthedocs.io/waits.html

Unable to locate text input element using selenium and python

I am trying to find an element on a webpage (https://www.fimea.fi/web/en/databases_and_registeries/spcs/human_medicinal_products) using Selenium and Python and I want to find the first text input for name of the medicinal product.
I am getting this error:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name='nimi'][#type='text']"}
I am currently using the code below:
clear_button = driver.find_element_by_xpath("//input[#name='nimi'][#type='text']")
I am new to Python and Selenium. Please help me out on this.
The field you are looking for is inside an iframe.
You need to identify and switch to that iframe first.
iframe=driver.switch_to.frame('_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_1UG85fOFT8Za_iframe')
input_field = driver.find_element_by_xpath("//input[#name='nimi']")
The reason why you can't locate the element is that the input box is hiden by a frame. Maybe you should use switch_to.frame() to switch to it.
F12 screenshot

Locate "file upload" button with Selenium

I have the button shown below (image and HTML) and am trying to click it.
Selenium is unable to locate it - I have tried locating by both xpath and by ID.
<input id="wsUpload1" type="file" name="file">
XPATH:
element = driver.find_element_by_xpath('//input[#id="wsUpload1"]')
element.click()
Where am I going wrong?
EDIT: Here is the exception thrown by Selenium:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id="wsUpload1"]"}
Possibilities
Duplicate web element with same id in the page.
Element may be in frame. You need to switch to frame
Trying to access the web element before page is loading.Give some wait time.
Not sure why your button wasn't found, maybe it's because of quotes (although it should show you error in that case), try with driver.find_element_by_xpath(".//input[#id='wsUpload1']") and see if it works. I'm not sure is your button already rendered on the page or you trigger it somehow so it's not there yet?
NoSuchElementException is thrown because your targeted element couldn't be found on that page, it could be that you are on the wrong page, element is not rendered yet so you should wait for it to appear, element could be in some iframe etc etc, it's hard to say when I don't know how your page works.
But if you are trying to upload something you should perform sendKeys() on that button (with path of file which are you trying to upload), not click() on it. This is how selenium upload works.
I have solved it - the driver opens a tab on a side panel and the button is in the tab. There seems to be a few ms delay between clicking the tab and the button appearing so I added a wait until element is clickable and that seems to work.
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='wsUpload1']"))).click()

Categories