Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
selenium click in iframe fails
#1
hi Guys,

What I want: clicking on the 'sign in' button on this website https://sites.google.com/site/ActiveRumblers/deck
I can get the input populated with data, but the click action ... nope. What am I doing wrong.


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:/Python27/include/chromedriver.exe")
import time

URL = "https://sites.google.com/site/ActiveRumblers/deck"
driver.get(URL)

#ensure page is loaded
time.sleep(3)

#login form is 4 iframes deep
driver.switch_to_frame(0)
driver.switch_to_frame(0)
driver.switch_to_frame(0)
driver.switch_to_frame(0)

#got xpath using google chrome, inspect, copy xpath
driver.find_element_by_xpath('/html/body/form/table/tbody/tr/td[1]/table/tbody/tr[2]/td[1]/div/input[3]').click()
driver.switch_to_default_content() 

time.sleep(2)
print "done"
Reply
#2
change line 19 (temporarily):
driver.find_element_by_xpath('/html/body/form/table/tbody/tr/td[1]/table/tbody/tr[2]/td[1]/div/input[3]').click()
to:
element = driver.find_element_by_xpath('/html/body/form/table/tbody/tr/td[1]/table/tbody/tr[2]/td[1]/div/input[3]')
print(element)
is element empty?
Reply
#3
hmmm yes, it is empty. so that explains the non click action.

Strange though since the xpath is correct (looking at the html of that nested iframe)

Altering the xpath to something like:
element = driver.find_element_by_xpath('//input[@value="SIGN IN"]')
# element by xpath = <selenium.webdriver.remote.webelement.WebElement (session="74feb924af78ffa24ec43105c5c80ffc", element="602b289d-b6ff-45e4-aeb7-3ed415cd942b")>
However when I add the click() to it ... nothing happens.
element = driver.find_element_by_xpath('//input[@value="SIGN IN"]')
element.click()
Reply
#4
(Bump)

Anybody a clue?
Reply
#5
how did you get the xpath, was it using inspect?
Reply
#6
(Apr-27-2020, 03:55 PM)Larz60+ Wrote: how did you get the xpath, was it using inspect?

Quote:'/html/body/form/table/tbody/tr/td[1]/table/tbody/tr[2]/td[1]/div/input[3]'
Was obtained using inspect.

Quote:'//input[@value="SIGN IN"]'
was done myself using info I found online.
Reply
#7
Quote:Was obtained using inspect.
I use mozilla firefox as my browser, and have encountered issues from time to time where XPATH appeared to be wrong,
What I do as a work around, is to get the XPATH of a major node the occurs just prior to the one wanted, and then traverse down the chain until I reach the tag that I want.
for example, if I want a specific tr tag in a table, I'll find the table rag, then switch over to beautifulsoup (using 'browser.page_source' to get starting source). Not a perfect way to do it, but it works.
Are you using Firefox?

here's a complete example where I do this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import BusinessPaths
import time
import PrettifyPage
import CreateDict
import json
import sys


class PreviewSearchPage:
    def __init__(self):
        self.bpath = BusinessPaths.BusinessPaths()
        self.pp = PrettifyPage.PrettifyPage()
        self.cd = CreateDict.CreateDict()

        self.analyze_page()

    def start_browser(self):
        caps = webdriver.DesiredCapabilities().FIREFOX
        caps["marionette"] = True
        self.browser = webdriver.Firefox(capabilities=caps)

    def stop_browser(self):
        self.browser.close()

    def save_page(self, filename):
        soup = BeautifulSoup(self.browser.page_source, "lxml")
        with filename.open('w') as fp:
            fp.write(self.pp.prettify(soup, 2))
    
    def analyze_page(self):
        self.start_browser()
        self.get_search_page('Andover')
        self.stop_browser()
    
    def get_search_page(self, searchitem):
        # pick city with multiple pages
        url = self.bpath.base_url
        self.browser.get(url)
        time.sleep(2)
        print(f'Main Page URL: {self.browser.current_url}')
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/form/div/div/span[1]/select/option[3]').click()
        searchbox = self.browser.find_element(By.XPATH, '//*[@id="query"]')
        searchbox.clear()
        searchbox.send_keys(searchitem)
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/form/div/div/span[3]/button').click()
        time.sleep(2)
        print(f'Results Page 1 URL: {self.browser.current_url}')
        # get page 2
        # find next page button and click
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[3]/div[2]/div/span[1]/a/icon').click()
        time.sleep(2)
        print(f'Results Page 2 URL: {self.browser.current_url}')
        # Get url of a detail page
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/table/tbody/tr[1]/td[1]/a').click()
        time.sleep(2)
        print(f'Detail Page URL: {self.browser.current_url}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Click on a button on web page using Selenium Pavel_47 7 4,507 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  Selenium suddenly fails to find element Pavel_47 3 6,151 Sep-04-2022, 11:06 AM
Last Post: Pavel_47
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,755 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 1,980 May-02-2020, 11:21 AM
Last Post: rajeev1729
  How to find which frame/iframe my element belongs to? smaria 1 2,021 Nov-18-2019, 09:21 PM
Last Post: Larz60+
  Python Selenium .click() Loads Error - Works Manually.- Events not Triggered NSearch 24 11,440 Aug-14-2019, 02:23 PM
Last Post: NSearch
  Selenium click on popup button??? GuJu 7 7,668 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Looping actions in an iframe using Selenium on Python amyd 3 2,936 Mar-06-2019, 08:31 PM
Last Post: Larz60+
  Click Element if displayed using Selenium and Python giaco__mar 1 3,469 Dec-27-2018, 06:19 PM
Last Post: metulburr
  scraping with multiple iframe jansky 1 4,152 Nov-09-2018, 11:12 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020