The popup
Trying to automate a YouTube search as I'm learning python automation, however I've come across a problem. I can't figure out how to automate clicking the "I agree" on the popup, hoping someone can send a solution
You can find the element using xpath and click on it with the click() method: https://pythonspot.com/selenium-click-button/
To switch to the iframe popup
driver.switch_to.frame("iframe")
//("iframe" for this specific example, otherwise put id of the iframe there)
driver.find_element_by_xpath('//*[#id="introAgreeButton"]/span/span').click()
To switch back to main frame
driver.switch_to.default_content()
browser.switch_to_default_content()
browser.switch_to.frame(browser.find_elements_by_tag_name('iframe')[0])
browser.implicitly_wait(1)
browser.find_element_by_id('introAgreeButton').click()
browser.switch_to.default_content()
I'm using 'browser' instead of 'driver' here.
Related
I'm trying to click on a button using Selenium with Python, however the button does not appear on Page Source and thus, the instantiated driver cannot find it. Here is the link I am accessing: https://data.ntsb.gov/carol-main-public/query-builder?month=6&year=2021
And the button I want to click is "Download Summary (CSV)", which is located at the end of the page.
I have already googled this kind of trouble but I haven't found a possible solution so far.
My attempt was to find the button directly by using the following commands, but it returns a NoSuchElementException:
element = driver.find_element_by_id('exportSummaryButton')
element.click()
This exception triggered me the intuition that the page source differs from what I see on Devtools and this is exactly what happens.
Thanks in advance!
I don't know what's exactly the thing that Amazon opens when you click the "buy now" button, I tryed to search on internet but couldn't find a clear answer. It could be a pop-up or an iframe, but they call it "popover" in their source code, which I have no clue what it is.
The point is, once you open this "popover", Selenium is unable to parse any part of that frame since it's a standalone separate HTML doc. I'm not sure if it's an iframe as I've never seen an iframe like that, so any guide I serached online about how to switch context using an Handler to an iframe doesn't work here. I couldn't find an ID, name, or anything significant to allow Selenium to parse the frame.
image showing the "popover"
If anyone has any clue how this HTML element is actually called and how to allow Selenium to parse it, please let me know, thanks.
I checked HTML of the page and here are my findings:
'Close' button is not in iFrame
Popup content itself is in iframe
See screenshot below:
What does it mean for you:
In order to click on the button 'Close' (e.g. by css selector button[class*='a-button-close']) you do not need to switch to any frames. If you are failing to click on the button - I would assume that you are missing selenium wait. There is a small delay after you clicking on "Buy Now" button and before popup actually loads - you need to explicitly wait for it to load. See https://selenium-python.readthedocs.io/waits.html for more details on selenium waits. I would recommend using visibility_of_element_located or visibility_of to wait for popup/button to appear (load) in your case.
If you did any operations in the popup itself (e.g. if you clicked on some links inside of it) - you most likely switched to the context of the iFrame with driver.switch_to.frame(iframe). In this case - you need to switch back to main context with driver.switch_to.default_content() in order to click on the 'Close' button (since it is outside of iframe).
I highly recommend learning runtime evaluate technique - it will save you hours on debugging your applications - see https://www.jetbrains.com/pycharm/guide/tips/evaluate-expression/
P.S. next time when asking question - please post your code (at least on high level) - it will help with localizing issue.
I'm trying to open a site with Selenium (with Python) using Chrome browser, but when I do, a full screen promo banner immediately pops-up and I can't access the site content unless I close it.
On the top right there is an "x" as if it was a quit button, but actually it's an element ::before
and from its description it seems to me that it doesn't contain any button element.
If I operate manually, both clicking on the x and on the upper part of the page outside the banner, the latter closes, but I really don't understand how to access it with selenium.
The webpage I'm trying to open is https://sports.bwin.it/it/sports
Needless to say I'm quite inexperienced, so I hope this question won't sound too basic, but I wasn't able to find a solutione in the selenium docs or on the web; if someone could give me any hint I would appreciate it.
This is a screenshot from the page I'm talking about
This is part of the html code from the web page; the element I am talking about is the one pointed by the arrow;
Based on your screen shot the xpath you want to use would be something like this:
//*[#data-id='dj_sports_c_ovl_br']//span
full code would be something like this:
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#data-id='dj_sports_c_ovl_br']//span"))
)
element.click();
I am making an Instagram bot using selenium and python. I am trying to implement a function which allows a certain amount of followers to be unfollowed. The code navigates to the unfollow screen which has many buttons that say unfollow. The buttons are hard to find with selenium though. If the find_element_by_tag('button') function is used only 3 buttons are found. The button html is:
<button class="_0mzm- sqdOP L3NKy _8A5w5 " type="button">Following</button>
For some reason, I cannot get selenium to select the element. Are there any ways someone can see how to click it?
Try switching to the active element and then clicking on the following
driver. switchTo() . activeElement() ;
And then try the xpath that you tried. This should work
Copy the xpath of the button tag and create reference and use for loop to iterate from one unfollow button onto the next, see my github repository
https://github.com/asadamatic/Unfollow-All-On-Instagram/blob/master/unfollowAll.py
I try to press a button using selenium. How do I click on a particular button using several conditions? (https://ibb.co/cJZxD7b) describing image
I have already tried something like this //span[text()=1.01], but what if webpage have few buttons with the same text.
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='45.00 7.75 1.04'] and //span[text()=1.04"))).click()
I expect to click on particular button
You can try with xpath
//span[#class='stn-val']
But it would probably be the best if you shared more of the HTML code because now I'm only guessing based on what I see.
Also, are you getting any error messages?
UPDATE:
Based on HTML posted in comments, use this selector:
//div[#class="st-col-bet-container"][2]/ul/li[3]/span
For tips on xpath selectors, see here.