Home » How to handle common Selenium Challenges Using Python?

How to handle common Selenium Challenges Using Python?

Photo by Olav Ahrens Røtne on Unsplash

How to download files using Selenium WebDriver

import os

download_dir = os.getcwd()# current working directory

profile = webdriver.FirefoxProfile()

profile.set_preference("browser.download.folderList", 2) # the custom location specified browser.download.dir is enabled when browser.download.folderList equals 2
profile.set_preference('browser.download.dir', download_dir)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", path)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', CONTENT_TYPE)
profile.set_preference("webdriver_enable_native_events", False)
profile.set_preference("browser.download.manager.scanWhenDone",False)
profile.set_preference("browser.download.manager.useWindow",False)
profile.set_preference("browser.helperApps.alwaysAsk.force",False)
profile.update_preferences()

browser = webdriver.Firefox(firefox_profile=profile)
Graphical interface to retrieve MIME-type

How to upload files using Selenium WebDriver

Solution 1

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from webdriver_manager.firefox import GeckoDriverManager

url = "my_url"
browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())
browser.get(url)

my_path = os.getcwd()
file_name = "MY_FILE_NAME"
input_file_xpath = "my_xpath"
full_path = os.path.join(my_path, file_name)

browser.find_element_by_xpath(input_file_xpath).send_keys(full_path + Keys.RETURN)

 

Solution 2

pip install -U pyautoit
import autoit

dialog_window_title = "File Upload"

browser.find_element_by_xpath(input_file_xpath).click()

autoit.win_active(dialog_window_title)
autoit.control_send(dialog_window_title, "Edit1", full_path)
autoit.control_send(dialog_window_title, "Edit1", "{ENTER}")

Screenshot by author

How to open multiple tabs and navigate through them using Selenium WebDriver

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from webdriver_manager.firefox import GeckoDriverManager

browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())
browser.get('https://medium.com')
wait = WebDriverWait(browser, 10)

results = wait.until(
    EC.presence_of_element_located((By.XPATH, '/html/body/div/div/div[3]/div/div[1]/div/div/div/div[3]')))
# The three first links on medium main page navigation bar
links = results.find_elements_by_tag_name('a')[:3]

# Open the link in a new tab by sending the following key strokes on the element : Keys.CONTROL + Keys.RETURN
for link in links:
    link.send_keys(Keys.CONTROL + Keys.RETURN)
    time.sleep(1)

# Get current tab
current_tab = browser.current_window_handle

# Get list of all tabs
tabs = browser.window_handles

for tab in tabs:
    # switch focus to each open tab one by one
    if tab != current_tab:
        # Switch to tab
        browser.switch_to.window(tab)
        time.sleep(1)

        # Get tab name
        title = browser.title

        # Close current tab
        print(f'Closing tab with title: "{title}"')
        browser.close()

browser.switch_to.window(current_tab)

Unreachable elements: Meet iFrames

from selenium import webdriver

from webdriver_manager.firefox import GeckoDriverManager


browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())


publication = "https://medium.com/python-in-plain-english"
article_name = "master-selenium-webdriver-with-python-in-10-minutes"
browser.get(f"{publication}/{article_name}-8affc8b931c")
password_xpath = "/html/body/div/div/div[1]/div/div/div/table/tbody/tr[10]/td[2]/span[3]"

element = browser.find_element_by_xpath(password_xpath)  # raises a NoSuchElementException
Screenshot by author
frame_xpath = "/html/body/div/div/div[3]/article/div/section[2]/div/div/figure[2]/div/div/iframe"
password_xpath = "/html/body/div/div/div[1]/div/div/div/table/tbody/tr[10]/td[2]/span[3]"

frame = browser.find_element_by_xpath(frame_xpath)
browser.switch_to.frame(frame)

password = browser.find_element_by_xpath(password_xpath).text
browser.switch_to.default_content()
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

browser.switch_to.default_content()
expected_condition = EC.frame_to_be_available_and_switch_to_it((By.XPATH, frame_xpath))
WebDriverWait(browser, 2).until(expected_condition)

password = browser.find_element_by_xpath(password_xpath).text
browser.switch_to.default_content()

Final Thoughts

About the Author

Amal Hasni

A Data Science consultant and a technology enthusiast eager to learn and spread the knowledge!

You can also read this article on our Mobile APP Get it on Google Play

2 Comments