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

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

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 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 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

Can't find element with Selenium even though I know the element exists

Okay, so I'm trying to find
<span class="totalcount">171</span>
on https://boston.craigslist.org/search/sss?query=food+stamps&sort=rel&search_distance=200&postal=01841
with
pagelement = driver.find_element_by_class_name('totalcount')
but for some reason I keep getting the following error
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == .totalcount
For reference, I'm using internet explorer 11 with Selenium because my boss requested I switch over to that from Firefox. Could that be what is causing the problem? (Before someone asks, I know it isn't because the page hasn't loaded yet, I added a wait specifically to deal with that.)
I did work for me, but im using find_elements as there are more than one element with the same locator strategy and also using chrome
not sure if it will help you, but you can use the below code for a better approach
Update: Tired with IE and its working
from selenium.webdriver import Ie
driver = Ie('path to IE driver')
driver.get('https://boston.craigslist.org/search/sss?
query=food+stamps&sort=rel&search_distance=200&postal=01841')
total_count = [x.text for x in driver.find_elements_by_class_name('totalcount')]
print(total_count)
['170', '170']

Categories