Header Ads

ad728
  • Breaking News

    Selenium Python Cheat Sheet



    Installation:

    pip install selenium


    Import Statements:

    from selenium import webdriver


    Launch Chrome Driver:
    driver = webdriver.Chrome()


    To Initialize WebDriver:

    # For Chrome

    driver = webdriver.Chrome()


    # For Firefox

    driver = webdriver.Firefox()


    # For Edge

    driver = webdriver.Edge()


    To open URL:

    driver.get("url")


    Navigation commands:

    # Go Forward

    driver.forward()


    # Go Back

    driver.back()


    # To Refresh page

    driver.refresh()


    To Find element:

    # Find by ID

    element = driver.find_element(By.ID, "element_id")


    # Find by Name

    element = driver.find_element(By.NAME, "element_name")


    # Find by Class Name

    element = driver.find_element(By.CLASS_NAME, "element_class_name")


    # Find by Tag Name

    element = driver.find_element(By.TAG_NAME, "element_tag_name")



    # Find by Link Text

    element = driver.find_element(By.LINK_TEXT, "link_text")


    # Find by Partial Link Text

    element = driver.find_element(By.PARTIAL_LINK_TEXT, "link_text")


    # Find by XPath

    element = driver.find_element(By.XPATH, "//path/to/element")


    # Find by CSS Selector

    element = driver.find_element_by(By.CSS_SELECTOR, "css_selector")


    Conditional commands:

    These statements return boolean values (True/False).


    driver.find_element(By.locator_name, "locator_value").is_displayed()

    driver.find_element(By.locator_name, "locator_value").is_enabled()

    driver.find_element(By.locator_name, "locator_value").is_selected()


    To get the title of the page:

    titile_of_the_page = driver.title

    print(titile_of_the_page)


    To get the current URL of the page:

    url_of_the_page = driver.current_url

    print(url_of_the_page)


    To click on an element:

    driver.find_element(By.locator_name, "locator_value").click()


    To close the currently focussed browser:

    driver.close()


    To close all the opened browsers:

    driver.quit()


    Implicit wait:

    - Applicable for all elements.

    driver.implicitly_wait(time_in_seconds)


    Explicit Wait:

    - Application for a specific element and for a specific condition


    from selenium.webdriver.commom.by import By

    from selenium.webdriver.support.ui import WebDriverWait

    from selenium.webdriver.support import expected_conditions as EC


    wait = WebDriverWait(driver, time_in_seconds)

    element = wait.until(EC.element_to_be_clickable((By.locator_name, "locator_value")))

    element.click()


    Interaction with Elements:

    # Type text into an input field

    element.send_keys("Text to input")


    # Click on an element

    element.click()


    # Get text from an element

    text = element.text


    # Clear text in an input field

    element.clear()


    # Working with dropdown

    element = driver.find_element(By.locator_type, "locator_value")

    dropdown = Select(element)


    # select by visible text

    dropdown.select_by_visible_text("dropdown_text")


    # select by index

    dropdown.select_by_index(2)


    # select by value

    dropdown.select_by_value("dropdown_value")


    Working with Alerts:

    # Switch to Alert:

    alert = driver.switch_to.alert


    # Accept or Dismiss Alert:

    # Accept (Click OK)

    alert.accept()


    # Dismiss (Click Cancel)

    alert.dismiss()


    # Get Alert Text:

    alert_text = alert.text



    # Send Text to Alert (if it is a prompt):

    alert.send_keys("Your text here")

    alert.accept()



    Working with Frames:

    # Switch to Frame by Index:

    driver.switch_to.frame(0)  # Index starts from 0


    # Switch to Frame by Name or ID:

    driver.switch_to.frame("frame_name_or_id")


    # Switch Back to Default Content:

    driver.switch_to.default_content()



    Working with Window Handles:

    # Get Current Window Handle:

    current_window_handle = driver.current_window_handle


    # Get All Window Handles:

    all_window_handles = driver.window_handles



    # Switch to a Different Window:

    # Assuming you have obtained the window handle of the target window

    driver.switch_to.window(target_window_handle)



    Scrolling:

    Scroll Down:

    # Scroll down by pixel

    driver.execute_script("window.scrollBy(0, 300);")


    # Scroll down to the bottom of the page

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



    Scroll Up:

    # Scroll up by pixel

    driver.execute_script("window.scrollBy(0, -300);")


    # Scroll up to the top of the page

    driver.execute_script("window.scrollTo(0, 0);")



    Scroll to Element:

    # Assuming 'element' is the WebElement you want to scroll to

    driver.execute_script("arguments[0].scrollIntoView(true);", element)



    Mouse Actions:

    import Actions:

    from selenium.webdriver.common.action_chains import ActionChains



    Perform Mouse Hover:

    # Assuming 'element' is the WebElement you want to hover over

    hover = ActionChains(driver)

    hover.move_to_element(element).perform()



    Perform Right-Click:

    # Assuming 'element' is the WebElement you want to right-click

    right_click = ActionChains(driver)

    right_click.context_click(element).perform()



    Perform Double-Click:

    # Assuming 'element' is the WebElement you want to double-click

    double_click = ActionChains(driver)

    double_click.double_click(element).perform()



    Perform Drag and Drop:

    # Assuming 'source' and 'target' are the source and target WebElements

    drag_and_drop = ActionChains(driver)

    drag_and_drop.drag_and_drop(source, target).perform()



    Perform Click and Hold:

    # Assuming 'element' is the WebElement you want to click and hold

    click_and_hold = ActionChains(driver)

    click_and_hold.click_and_hold(element).perform()



    Release Click After Hold:

    # Assuming 'element' is the WebElement you want to click and hold, then release

    click_and_hold = ActionChains(driver)

    click_and_hold.click_and_hold(element).release().perform()



    Hey there! Drop a comment and share your thoughts on this post. Your feedback is valuable, and I appreciate you taking the time to let me know what you think!

    Thanks,

    Sainadh


    1 comment:

    1. Selenium Python Cheat Sheet | Selenium Python Commands | Cheat Sheet for Selenium Python

      ReplyDelete