Home > Software design >  How to press accept button with selenium webdriver?
How to press accept button with selenium webdriver?

Time:10-22

This is the button I want to press:

enter image description here

def obtenerCoordenadasDirección():

   header = {"User-Agent": "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}
   driver = webdriver.Chrome('chromedriver.exe')
   driver.maximize_window()

   adresses="Calle Urb. La Arbor"
   driver.get('https://www.google.com/maps/search/' adresses)

   print(driver.current_url)

   driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
   time.sleep(1)

   driver.find_element_by_css_selector('body > div.box > div:nth-child(5) > form > 
   input.button').click()


obtenerCoordenadasDirección()

I tried looking for id,class,xpath and value but it didn't work. Also I saw that it`s because the iframe so I need to change it but I don't really know how to do it. In the code above I tried with css.selector but it neither work.

CodePudding user response:

2 consideration generally for selenium.

Frames:

iframe = driver.find_element_by_xpath("//iframe[@name='Dialogue Window']")
driver.switch_to.frame(iframe)

Page load: selenium is actual code and many times runs faster than the page gets loaded.So you need to wait the actual button gets loaded first.example:

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "tag_id"))
    )
finally:
    driver.quit()

CodePudding user response:

Using xpath to find the button worked for me:

from selenium import webdriver
import time

driver = webdriver.Chrome('C:/Chromedriver/chromedriver.exe')
driver.get('https://www.google.com/maps/search/Calle Urb. La Arbor')

driver.maximize_window()

time.sleep(1)  #wait for site to load

driver.find_element_by_xpath('/html/body/c-wiz/div/div/div/div[2]/div[1]/div[4]/form/div[1]/div/button/span').click()
  • Related