Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
selenium click in iframe fails
#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


Messages In This Thread
selenium click in iframe fails - by 3Pinter - Apr-20-2020, 06:49 PM
RE: selenium click in iframe fails - by Larz60+ - Apr-20-2020, 09:03 PM
RE: selenium click in iframe fails - by 3Pinter - Apr-21-2020, 06:18 AM
RE: selenium click in iframe fails - by 3Pinter - Apr-27-2020, 08:35 AM
RE: selenium click in iframe fails - by Larz60+ - Apr-27-2020, 03:55 PM
RE: selenium click in iframe fails - by 3Pinter - Apr-29-2020, 07:32 AM
RE: selenium click in iframe fails - by Larz60+ - Apr-29-2020, 12:59 PM

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