I try to open a browser and click on a button by using a python script
My code:
from selenium import webdriver
browser = webdriver.Chrome('/usr/local/bin/chromedriver')
browser.get('xxx')
browser.implicitly_wait(5)
button = browser.find_element_by_css_selector('#softGateBox > div.button_softgate > a')
button.click()
The website opens. It waits 5 seconds and then I see the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#softGateBox > div.button_softgate > a"}
What can be wrong? I use chrome to inspect the button and I perform a right click and click copy selector.
There is few problem with this page
element is inside <iframe> so you have to find <iframe> and switch_to_frame() before you can search element
<iframe> is in external <iframe> so first you have to find external <iframe> and switch_to_frame() before you start to searching internal <iframe>
on small monitor element is invisible so Selenium can click it. You have to scroll page to element and then you can click it.
.
from selenium import webdriver
browser = webdriver.Chrome() #'/usr/local/bin/chromedriver')
browser.get('https://www.facebook.com/SparColruytGroup/app/300001396778554?app_data=DD722A43-C774-FC01-8823-8016BFF8F0D0')
browser.implicitly_wait(5)
iframe = browser.find_element_by_css_selector('#pagelet_app_runner iframe')
browser.switch_to_frame(iframe)
iframe = browser.find_element_by_css_selector('#qualifio_insert_place iframe')
browser.switch_to_frame(iframe)
button = browser.find_element_by_css_selector('#softGateBox > div.button_softgate > a')
browser.execute_script("arguments[0].scrollIntoView(true);", button)
button.click()
BTW:
There can be other <iframe> on page so you can't direclty do selector('iframe').
Internal frame have id but it changes every time you load page so you can't do selector('iframe#some_uniqe_id')
Related
Anybody has a solution to locate a button in webpage with an overlayed popup window like in the following example:
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'./geckodriver')
driver.get("https://www.academics.de/")
#after waiting for a while the popup window comes up
driver.find_elements_by_xpath("//*[contains(text(), 'Zustimmen')]")
The returned list is empty. Running the following
driver.find_element_by_css_selector(".button-accept")
results in:
NoSuchElementException: Message: Unable to locate element: .button-accept
The element with the text as E-Mail Login is within an iframe so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.academics.de/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Zustimmen']"))).click()
Using XPATH:
driver.get("https://www.academics.de/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Zustimmen']"))).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
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
An easy workaround to your problem would be to use uBlock Origin extension + Fanboy's Annoyances blocklist on your Selenium instance so that these annoying cookies messages would outright never appear. A way of enabling extensions is described in this StackOverflow answer:
Create a new firefox profile via right click windows start button >
run > firefox.exe -P
Then add whatever extensions you want, ublock, adblock plus etc
Call your profile folder with
profile = selenium.webdriver.FirefoxProfile("C:/test")
browser = selenium.webdriver.Firefox(firefox_profile=profile, options=ops)
I am working on a selenium script in Python, where I am on this stage trying to locate a game icon.
But I can't locate it and click.
enter image description here
This is what I've tried:
self.driver.find_element_by_xpath('/html/body/div[5]/ul/li[17]/a/img')
self.driver.find_element_by_xpath("//*[#id="jackpot_01001"]")
self.driver.find_element_by_xpath('//*[#id="gameList_SLOT_01001"]')
self.driver.find_element_by_id("jackpot_01001").click()
But it will show:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/ul/li[17]/a/img"}
(Session info: chrome=93.0.4577.82)
This error
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/ul/li[17]/a/img"} (Session info: chrome=93.0.4577.82)
implies that Either one of the following :
Element locator is not correct.
Element is in iframe.
Element is in shadow root.
I am not sure if //li[#id='gameList_SLOT_01001'] or /html/body/div[5]/ul/li[17]/a/img is unique or not. You need to check in HTML DOM.
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
If it happens to be a unique node, then put some waits and then perform the click.
Update 1:
There are 4 ways to click in Selenium.
I will use this xpath
//li[#id='gameList_SLOT_01001']/descendant::img
Code trial 1:
time.sleep(5)
self.driver.find_element_by_xpath("//li[#id='gameList_SLOT_01001']/descendant::img").click()
Code trial 2:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#id='gameList_SLOT_01001']/descendant::img"))).click()
Code trial 3 :
time.sleep(5)
button = self.driver.find_element_by_xpath("//li[#id='gameList_SLOT_01001']/descendant::img")
self.driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = self.driver.find_element_by_xpath("//li[#id='gameList_SLOT_01001']/descendant::img")
ActionChains(self.driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
Update 2 :
Since it's a new tab, you would have to switch to it first before interacting with any web element on this new tab.
self.driver.find_element_by_id('RTGWEB-RTGWEB').click()
time.sleep(5)
self.driver.switch_to.window(driver.window_handles[1])
time.sleep(5)
self.driver.find_element_by_xpath("//li[#id='gameList_SLOT_01001']/descendant::img").click()
I am new to web scraping and I am trying to scrape reviews off amazon.
After going on a particular product's page on amazon I want to click on the 'see all reviews' button. I did inspect element on the page, I found that the see all reviews button has this structure
structure
So I tried to find this element using the class name a-link-emphasis a-text-bold.
This is the code I wrote
service = webdriver.chrome.service.Service('C:\\coding\\chromedriver.exe')
service.start()
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
options = options.to_capabilities()
driver = webdriver.Remote(service.service_url, options)
driver.get(url)
sleep(5)
driver.find_element_by_class_name('a-link-emphasis a-text-bold').click()
sleep(5)
driver.implicitly_wait(10)
But this returns me the following error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".a-link-emphasis a-text-bold"}
What am I doing wrong here?
driver.find_element_by_class_name('a-link-emphasis.a-text-bold').click()
By class expects single class not multiple but you can use the above syntax , remove space with . as it uses css under the hood, or use :
driver.find_element_by_css_selector('.a-link-emphasis.a-text-bold').click()
driver.find_element_by_css_selector('[class="a-link-emphasis a-text-bold"]').click()
I want to select the first item of the combobox "Region" with this script in this website LINK Python + Selenium
chromeOptions = webdriver.ChromeOptions()
prefs = {'profile.managed_default_content_settings.images':2, 'disk-cache-size': 4096 }
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
url = 'https://www.tgr.cl/certificado-pago-deudas-contribuciones-tramite/'
driver.get(url)
driver.switch_to.frame(driver.find_element_by_name('busqueda'))
driver.find_element_by_xpath("//select[#name='region']/option[text()='REGION DE ANTOFAGASTA']").click()
But im getting this Error when Im switching the frame. Output:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="busqueda"]"}
Any ideas why is this happening?
All subdocuments like the <iframe> your element is in lazyload into existence slower than your entire webpage. It might be best to wait a few seconds, or until Selenium detects the <iframe>.
# Wait specifically for that iframe, then switch to it
from selenium.webdriver.support.ui import WebDriverWait
iframe_detector = WebDriverWait(driver, 5).until(lambda x: x.find_element_by_name('busqueda'),
message="Iframe never loaded!")
driver.switch_to.frame(iframe_detector)
#Just wait and hope it loads in
driver.implicitly_wait(5)
driver.switch_to.frame(driver.find_element_by_name('busqueda'))
You may need longer wait times to account for your internet connection.
The problem was that the combobox is the iframe of an iframe. So you needed to switch to the first iframe and after to the his child in that way you will be OK.
I am unable to close a popup box with Selenium. Below is the code I have written, it returns an exception. Please see the code below.
driver = webdriver.Chrome()
driver.get("https://www.google.com/webhp#q=home+depot+san+francisco&lrd=0x808f7c5c63124c7b:0x32c19e9988b2aa90,1,")
driver.find_element_by_xpath('//div[#class="_wzh"]').click()
# selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
Thanks,
It take some time to open your popup. So you need to wait for few seconds until the popup opens and close button get visible.
User Explicitwait condition until visibility of element like below :
element = WebDriverWait(driver, 60).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div>._wzh"))
)
element.click()
Instead of trying to click on element, you should use the built-in support.
Following should work.
alert = driver.switch_to_alert()
alert.accept
Docs
this is because the element is present on an <iframe> tag; so first you need to switch to iframe and then interact with the element:
driver.switch_to.frame driver.find_element_by_css(' #gsr > iframe')
then click on the element:
driver.find_element_by_xpath('//div[#class="_wzh"]').click()