How To Select Dropdown Value In Selenium (2 Min) Using Python

Описание к видео How To Select Dropdown Value In Selenium (2 Min) Using Python

In this tutorial, you'll learn how to select dropdown value in Selenium using Python and PyTest.

Facebook:   / gokcedbsql  

Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to select a value from a drop-down in selenium using Python. Let's start by looking at the test scenario. I want to go to amazon.com and then select Alexa skills from the drop-down.

Next, I want to type the keyword game in the search box and then hit the search submit button. Finally, I want to assert whether the keyword game appears in the first search result or not. Now let's look at our test directory I have a conf test.py file that contains a driver fixture function and this function is responsible for initializing the chrome web driver.

In my test file on line 6, I'm using the driver.get method to go to amazon.com. On line 8, I'm using the select class and select and select by visible text method to select Alexa skills from the drop-down. On line 11, I'm using the send keys method to type the keyword game in the search text box line.

Line 13, I'm clicking on the search submit button. On line 15, I'm using the get attribute method to get the inner HTML text of the first search result. Finally, on line 20, I'm asserting whether the keyword game appears in the text or not.

Now let's run this test to see what the execution looks like as you can see the test passed. Watch what happens if I change the keyword game to XYZ and re-execute the test. This time our test failed as expected due to an assertion error.

Finally, export the test results button. There you have it. Make sure you like, subscribe, and turn on the notification bell.

Until next time. Make sure you like, subscribe, and turn on the notification bell. Until next time.

Connect MySQL In Python:    • How To Connect To MySQL In Python (2 ...  
Run Selenium PyTests In Parallel:    • How To Run Selenium PyTests In Parall...  
Find_elements() in Selenium:    • How To: Find_Elements() In Selenium (...  


from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By


def test_dropdown(driver):
driver.get("https://www.amazon.com/")

Select(driver.find_element(
By.XPATH, '//*[@id="searchDropdownBox"]')).select_by_visible_text('Alexa Skills')

driver.find_element(By.XPATH, '//*[@id="twotabsearchtextbox"]').send_keys('game')

driver.find_element(By.XPATH, '//*[@id="nav-search-submit-button"]').click()

text = driver.find_element(By.XPATH, '//*[@id="search"]/div[1]/div[1]/div/span[3]'
'/div[2]/div[4]/div/div/div/div/div/div[2]'
'/div/div/div[1]/h2/a/span').get_attribute('innerHTML')

print("text:", text)
assert 'xyz' in text.lower()

import pytest
from selenium import webdriver
import os


@pytest.fixture()
def driver():
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
driver = webdriver.Chrome(executable_path=root_dir + '/resources/chromedriver')
driver.implicitly_wait(2)
yield driver
driver.quit()

Комментарии

Информация по комментариям в разработке