How to close pop up window in Selenium - python

Can't close pop up window which appears right after http://www.cargo.lt/ loads. Here's what I've got:
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
time.sleep(10)
driver.find_element_by_xpath('/html/body/div[36]/div/a').click()
I'm not very familiar with how to write custom xpath/css path and now just clicked on Inspect element and copied xpath. What I'm doing wrong?
EDIT:
What a stupid mistake. Didn't realize that when element is off screen Selenium can't click on it. Just added driver.maximize_window() and all my problems are gone. Thanks all for your answers. Unfortunately I can't vote yet, because I don't have enough points...

hi to close pop up /alert please use
driver.switch_to_alert()
then use
driver.find_element_by_xpath('/html/body/div[36]/div/a').click()
// if u have copied pasted form firebug then it will be correct i guess
or if ur xpath is not correct then use
driver.find_element_by_xpath("//a[#id='advert_x']").click()

i tried your code it's work fine i don't know whats you problem but
try this : driver.implicitly_wait(10)
insted of :
time.sleep(10)

You can use the Predefined commands in python -Selenium to switch to alert box.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
alrt = driver.switch_to_alert()
alrt.accept()
Hope it Helps.

you can use chrome options
in Python, you can use
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-notifications")
webdriver.Chrome(os.path.join(path, 'chromedriver'),chrome_options=chrome_options)
in java
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("test-type");
ChromeDriver driver = new ChromeDriver(options);

Related

Webdriver wont click on a second link

I want to make a bot using selenium, but I'm having trouble with my bot going to a different part of a website. In my code, my driver successfully goes to nike.com (1), then successfully clicks and loads a different link within Nike (clicks circled area in (1) and goes to (2)). Then, my problems begin here, I try to click and load a different link (2) but my driver does nothing. I know my driver found the second link because if I print out 'second.text' then I get the correct text (3)...
I am still new to selenium and I pretty much don't know what I am doing. Any help would be helpful.
Thank you.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
if __name__ == '__main__':
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.nike.com/men")
driver.implicitly_wait(5)
first = driver.find_element(by=By.CLASS_NAME, value="prl3-sm")
first.click()
driver.implicitly_wait(5)
second = driver.find_element(by=By.CSS_SELECTOR, value='a[class="JSftBPEZ"]')
#print(second.text)
second.click()
I have tested it.
Through Javascript click its getting clicked.
here is the code to click on second link.
second = driver.find_element(by=By.CSS_SELECTOR, value='a[class="JSftBPEZ"]')
driver.execute_script("arguments[0].click();",second)
BTW you may need to define xpath properly. Example the second link pointing to 6 elements. But anyway through Javascript sclick it would click the 1st option

How to pass the RGPD popup / iframe with Selenium (Python)?

Ok, attempt to use Selenium on a website : lefigaro.fr, but no class related to the RGPD popup to be found by Selenium, even after a switch to frame. :/
I'm juste looking a reliable way to close it.
It goes this way :
from selenium import webdriver
WINDOW_SIZE = "1920,1080"
ADRESSE = 'https://www.lefigaro.fr/'
driver = webdriver.Firefox() #the chrome version was even worse, geckodriver a the root
driver.get(ADRESSE)
elt = driver.find_element_by_class_name("sc-18sn7k8-1") #error
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
elt = driver.find_element_by_class_name("sc-18sn7k8-1") #error
You can switch to iframe via index.
It seems that popup is the only iframe on the page, so perhaps this will work:
driver.switch_to.frame(0) # or perhaps (1)
EDIT:
It seems the page already is in an iframe when it loads.
Do driver.switch_to.default_content() first.
Then it seems the iframe you want is (3):
driver.switch_to.frame(3)
Hope this works.
Hmm, actually, not quite... I'll spend some more time on this.

Python Selenium "Unable to locate element"

I am trying to use Selenium to sign up an email account automatically whenever I need to. It's just a fun learning project for me. For the life of me I don't understand why it can't find the element. This code works fine on the sign-in page but not the sign-up page. I have tried all different Selenium commands and even tried using the ID and class name. Either is says it can't locate the element or that it is not reachable by keyboard.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
driver.get("https://mail.protonmail.com/create/new?language=en")
time.sleep(10)
username_input = driver.find_element_by_id("username").send_keys("testusername")
Also here is the HTML code: https://i.imgur.com/ZaBMTzG.png
The username field is in iframe, you need to switch to iframe to make this work.
Below is the code that works fine :
driver.get("https://mail.protonmail.com/create/new?language=en")
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='top']"))
driver.find_element_by_id("username").send_keys("some string")
read more about iframe here
learn more about how to switch to iframe/frame/framset using Python
selenium Bindings here
Update :
wait = WebDriverWait(driver, 30)
driver.get("https://mail.protonmail.com/create/new?language=en")
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='top']"))
driver.find_element_by_id("username").send_keys("some string")
driver.switch_to.default_content()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(5)
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='bottom']"))
wait.until(EC.element_to_be_clickable((By.NAME, "submitBtn"))).click()
I'm not sure if I've seen enough code to diagnose, but I think the way you are defining username_input seems problematic. driver.find_element_by_id("username").send_keys("testusername") doesn't actually return anything so it seems like you are setting username_input = null.

Selenium: Follow adfly redirect

Im trying to make a bot that visits my adfly link using the chrome webdriver. Every time I try to use the code below though, the webdriver tells me that there were too many redirects and doesn't follow through. The code below is just being used for testing at the moment:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server="+"http://102.129.249.120:8080")
browser = webdriver.Chrome(options=options)
browser.get("http://raboninco.com/18Whc")
Image of error here
Okay so i figured it out. I can use tor with selenium to get access to adfly. Works great btw. Thanks for the help and time guys. If you want to see the code I used, here it is:
from selenium import webdriver
import os
os.popen(r'C:\Users\joldb\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server="+"socks5://127.0.0.1:9050")
browser = webdriver.Chrome(options=options)
browser.get("http://raboninco.com/18Whc")

Selenium won't open a new URL in a new tab (Python & Chrome)

I want to open quite a few URLs in different tabs using Selenium WebDriver & Python.
I am not sure what is going wrong:
driver = webdriver.Chrome()
driver.get(url1)
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
url2 = 'https://www.google.com'
driver.get(item2)
I looked up tutorials and it seems to me as though this code should do what I want. What actually happens is the browser opens, url1 opens as it should, a new tab opens as it should but url2 then loads in the original tab instead of the new one (even though the new tab appears to be the active one).
(I am using Chrome because when using Firefox I can't get it to load any URLs at all. Firefox opens but does not get the url requested. I have tried to find a solution to this but to no avail.)
Is there anything I can change in my code to get the new URL to open in the new tab?
Thanks for your help!
Here is a simple way, platform independent:
Code:
driver.execute_script("window.open('http://google.com', 'new_window')")
Switching back to the original tab:
Code:
driver.switch_to_window(driver.window_handles[0])
Checking the current title to be sure you are on the right page:
Code:
driver.title
For everything else, have fun!
There is a bug in ChromeDriver that prevents ctrl/command+T from working:
I canĀ“t open new tab in ChromeDriver
What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.google.com")
# open a link in a new window
actions = ActionChains(driver)
about = driver.find_element_by_link_text('About')
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()
driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")
Now the last driver.get() would be performed in a newly opened tab.
An alternative way to open a new window is to use JavaScript and the window handler to switch between them.
driver = webdriver.Chrome()
# Open a new window
# This does not change focus to the new window for the driver.
driver.execute_script("window.open('');")
# Switch to the new window
driver.switch_to.window(driver.window_handles[1])
driver.get("http://stackoverflow.com")
# close the active tab
driver.close()
# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])
driver.get("http://google.se")
# Close the only tab, will also close the browser.
driver.close()
If you look at your browser while you're executing it will look like the new window has focus, but to the webdriver, it doesn't. Don't be fooled by the visual. Also remember to select a new window handler when you close a tab as it will set the driver.current_window_handle to
selenium.common.exceptions.NoSuchWindowException:
Message: no such window: target window already closed from unknown error: web view not found
(Session info: chrome=<Your version of chrome>)
(Driver info: chromedriver=<Your chrome driver version> (<string of numbers>),platform=<Your OS>)
on .close() and it will throw that error if you try to do stuff with the driver at that stage.
you need to maximize your chrome for this
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/")
e = driver.find_element_by_tag_name("body")
ActionChains(driver).key_down(Keys.CONTROL).click(e).send_keys("k").key_up(Keys.CONTROL).perform()
here key_down(Keys.CONTROL) will hold down ctrl key, to get focus on page i am clicking body of the page, then click k

Categories