2

I have the following Code that goes to a URL(www.example.com), and clicks on a link(Example 1). (This part works fine)

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.example.com")

link = driver.find_element_by_link_text('Example 1')
link.click()

Now, when we click on 'Example 1' link, it opens a confirmation window, with 2 buttons: 'Yes I am authorized user to this site' and 'No I am a new visitor to this site'

So, I wish to click on 'Yes I am authorized user to this site' and then finally enter my log-in credentials. I have written these 2 lines, just below the above code, for clicking on that button. But these don't work.

button = driver.find_element_by_name("'Yes I am authorized user to this site'")
button.click()
16
  • Does the button have the quotation marks on it? Because based on your code you're saying the button says 'Yes I am authorized user to this site' Commented Aug 24, 2016 at 14:10
  • Yes, it has those quotation marks Commented Aug 24, 2016 at 14:11
  • Is there any way you can provide the link to the website, or at the very least screenshots because I don't really see anything wrong with your code. It might have something to do with the site itself. Commented Aug 24, 2016 at 14:13
  • I have also tried : (1) driver.find_element_by_link_text (2) driver.find_element_by_class_name . But none of these works Commented Aug 24, 2016 at 14:15
  • Can you provide a link? Commented Aug 24, 2016 at 14:16

3 Answers 3

5

If it is an alert window, you need to use the Alert command.

#import Alert
from selenium.webdriver.common.alert import Alert
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.example.com")

link = driver.find_element_by_link_text('Example 1')
link.click()
Alert(driver).accept()
#to dismiss alert
#Alert(driver).dismiss()

I think this would have solved your query.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please share the link where you are trying this?
Works perfectly in my case!
1

Based on the comment conversation, I would recommend both using an XPATH search (instead of Name or Id) and waiting for elements to be clickable or loaded. When web-driving or web-scraping, pages may intentionally or accidentally load slowly and this can cause issues if you have pauses or waits either hard coded or non-existent. This snippet of code should allow you to search Google using Selenium and Chromedriver (you can modify the driver function to use Firefox or something else if you'd like):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver.chrome.options import Options
from time import sleep

def init_driver(drvr_path):
    chrome_options = Options()
    chrome_options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(drvr_path+'chromedriver.exe',chrome_options=chrome_options)
    driver.wait = WebDriverWait(driver, 5)
    return driver

def lookup(query, driver=None, drvr_path=''):
    driver = None
    if driver is None:
        driver = init_driver(drvr_path)
    driver.implicitly_wait(45) # Allow up to 45 Seconds for page to load
    driver.get("http://www.google.com")
    try:
        box = driver.wait.until(EC.presence_of_element_located((By.XPATH, """//*[@id="lst-ib"]""")))
        box.send_keys(query)
        sleep(3) # Let you see the window open
        button = driver.wait.until(EC.element_to_be_clickable((By.XPATH,"""//*[@id="sblsbb"]/button""")))
        try:
            button.click()
        except ElementNotVisibleException, s:
            print "Error Handled: "+str(s)
            button = driver.wait.until(EC.element_to_be_clickable((By.XPATH,"""//*[@id="sblsbb"]/button""")))
            try:
                button.click()
            except:
                print "Could not search Google..."
                return
        resp=driver.page_source.encode('utf-8')
        with open(query+'.html','wb') as f:
            f.write(resp)
            print 'Wrote the File...'
    except:
        print("Box or Button not found in google.com")
    driver.quit()

For example, if your Chromedriver.exe file was located in your default Python path, you could do something like: lookup('Selenium Python XPATH Examples') and it should download an HTML file of the Google Search results. If you already have a Driver initialized, you could of course pass that to it.

Hope this helps

Comments

0

Try this code, hope it will help you

from selenium import webdriver
import time

driver = webdriver.Chrome('path to chromedriver\chromedriver.exe')
driver.get('https://www.example.com')
driver.maximize_window()

link = driver.find_element_by_link_text('Example 1')
link.click()
handles =driver.window_handles # this will give window handles

driver.switch_to.window(handles[1])
button = driver.find_element_by_name("'Yes I am authorized user to this site'")
button.click()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.