Back to Browse

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

1.6K views
Apr 23, 2022
2:13

In this tutorial, you'll learn how to select dropdown value in Selenium using Python and PyTest. — Facebook: https://www.facebook.com/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: https://www.youtube.com/watch?v=0qBctY82et0 Run Selenium PyTests In Parallel: https://www.youtube.com/watch?v=cQE6ABZ9I14 Find_elements() in Selenium: https://www.youtube.com/watch?v=uqWc_WyfTCI — 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()

Download

1 formats

Video Formats

360pmp43.8 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

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