Unable to locate text input element using selenium and python - 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

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 element with Selenium on a website

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

Unable to locate the element while using selenium-webdriver

I am very much new to selenium WebDriver and I am trying to automate a page which has a button named "Delete Log File". Using FireBug I got to know that, the HTML is described as
and also the css selector is defined as "#DeleteLogButton" using firepath
hence I used
browser.find_element_by_css_selector("#DeleteLogButton").click() in webdriver to click on that button but its now working and also, I tried,
browser.find_element_by_id("DeleteLogButton").click() to click on that button. Even this did not find the solution for my problem...
Please help me out in resolving the issue.
Most of the times im using By.xpath and it works specially if you use contains in your xpath. For example : //*[contains(text(),'ABC')]
This will look for all the elements that contains string 'ABC'
In your case you can replace ABC with Delete Log File
try to find it by name like :
browser.find_element_by_name("Delete Log File").click();

Categories