So, I'm working on a Selenium webdriver Project, and, in one of the process, I needed to enter in na iframe, and then, send Keys to the input in there, but, now I want to close or exit that iframe to can click in a button outside that frame, check out me code piece:
WebDriverWait(bot,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.top[title='Registration form']")))
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="username"]'))).send_keys(self.usernameChar)
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="username"]'))).send_keys(Keys.RETURN)
Can you guys help me?
Note: My google version is the 85
To exit from any <iframe> and set Selenium's focus on the Top Browsing Context you can use the following line of code:
driver.switch_to.default_content()
Reference
You can find a couple of relevant discussions in:
How to send text to the Password field within https://mail.protonmail.com registration page?
Related
I am trying to submit a form to create a new reddit account using selenium at https://www.reddit.com/reddits/login/ . I am new to using selenium and web scraping in general and need some help selecting and ultimately clicking the sign up button on this webpage after all the details have been entered in. I have tried to select it using
submit = browser.find_element(By.CSS_SELECTOR("//*[#id="register form"]/div[7]/button")).text
along with trying to grab it through its xpath which is
//*[#id="register-form"]/div[7]/button
but am having no luck. The farthest I am able to get is having it say a 'str' object is not callable using Selenium through Python as an error message or it will say element not found. Any help would or guidance in the right direction would be greatly appreciated.
I am able to enter in all fields of data and beat the re-captcha that pops up also and be entirely good to go except for this sign up button. I am not familiar with this much so I am not sure why any of the responses answers worked when I tried them. I am not sure if it is something about the button or if its just how the page is setup or if its needing to run some javascript with the button click. I am not sure why this button cannot be clicked.
To solve the re-captcha I load another webpage and delete it to get back to this one not sure if this is relevant or not.
Picture of the Button I am trying to click on with selenium
Sign Up Button Picture
SOLVED: All of these solution below work for accessing the button. My mistake was switching to a different tab and not properly switching back. So I was trying to click an element on an old tab not the main sign in page. Once the focus was shifted back to that page the element was found and clicked!
The CSS Selector for that submit button would be:
"#register-form button"
You can verify from the console of a web browser by calling:
document.querySelector("#register-form button")
XPATH worked for me:
url = driver.find_element(By.XPATH , '//*[#id="register-form"]/div[7]/button')
url.click()
wait = WebDriverWait(driver, 30)
driver.get("https://www.reddit.com/reddits/login/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#user_reg'))).send_keys('user')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#passwd_reg'))).send_keys('pw')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#passwd2_reg'))).send_keys('pw')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#email_reg'))).send_keys('user#email.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#register-form > div.c-clearfix.c-submit-group > button'))).click()
The full signup for that would be like so you would want to click the element of button. Replace keys with values you need.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
On Python 3.9 and Selenium 4.00
Hi there, I'm currently trying to automate downloading a few things on Chrome. I got the login part and navigating to the page down and it works properly. I'm having issues with the next part which is clicking "export" then "export as csv". I hover over the HTML source code and it highlights the buttons I need to press so I hit "copy XPath" but selenium won't press it and I get this error.
Edit: I cannot share the site as it is locked behind a login and it is not my login to give out; end of edit.
Message: invalid selector: Unable to locate an element with the xpath expression //*[#id="report_nav_menu"]/ul/li[2]/a"
Here's my code
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('website')
driver.find_element(By.XPATH, '//*[#id="report_nav_menu"]/ul/li[2]/a').click()
time.sleep(1) # makes sure the page loads
driver.find_element(By.XPATH, '//*[#id="report_nav_menu"]/ul/li[2]/ul/li[6]/a').click()
time.sleep(1000) # to keep the browser open
This the is HTML source code:
Source code
The first highlight in the pic is for the Export button.
Need to click this first
The second highlight shows that it's for the CSV button.
Need to click this second
//class[#elname="zc-navmenuEl/button[2] seems to be an invalid XPath expression.
I can't see this locator used in the code you presented in the question.
Also you didn't share an URL of the page you are working with so I can't determine the correct element locator.
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();
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.