How To: XPath Contains Function (2 Min) In Selenium Using Python | In PyTest & PyCharm
In this tutorial, you'll learn how to use the XPath contains function in Selenium using Python in PyTest & PyCharm.
—
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 use XPath content in Selenium using Python. Let's start by looking at the scenario.
I want to go to amazon.com and search for the keyword sunglasses. In the search result, I want to click on the product that has the label amazon's choice. Next, I want to assert whether the keyword UV400 protection appears on the product page.
Next, let's look at the test directory. I have a conf test.py file that contains the driver fixture function. This function initializes the Chrome driver and returns the driver object in my test file.
I'm passing the driver fixture as the first argument to my test function. On line 9, I'm using the get method to go to amazon.com. Line 11, I'm finding the search text box by id and typing the sunglasses keyword in it.
On line 13, I'm clicking on the search submit button. On line 15, use the XPath containing the function to search for any id that contains the keyword amazon's choice label and click on it. Next, I'm asserting whether the keyword UV400 protection appears in the paid source or not.
Finally, on line 20, I'm closing the browser. Let's run this test to see what the execution looks like. Note, I'm only using time dot sleep to artificially slow down the execution.
As you can see the test passed. Let's see what happens if I change the keyword to UV500 instead of UV400. As you can tell this time the test failed as expected.
Now let's look at how to find the web element ids from the Chrome browser. Start by clicking on the button with three dots then choose developer tools from the more tools drop-down. Now click on the inspect button then hover over the web element you are interested in.
Now click on it to get the corresponding HTML code and grab the ID from there. There you have it. 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
—
import pytest
from selenium import webdriver
import os
@pytest.fixture(scope="session")
def driver():
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
driver = webdriver.Chrome(executable_path=root_dir + '/resources/chromedriver')
# Time in seconds driver should wait when searching for an element if it is not
# immediately present
driver.implicitly_wait(2)
return driver
import pytest
import time
from selenium.webdriver.common.by import By
# pass the driver fixture as the first argument
@pytest.mark.test_keyword_in_search_result
def test_keyword_in_search_result(driver):
driver.get("https://www.amazon.com/")
time.sleep(2) # only used for the demo
driver.find_element(By.ID, "twotabsearchtextbox").send_keys("sunglasses")
time.sleep(2) # only used for the demo
driver.find_element(By.ID, "nav-search-submit-button").click()
time.sleep(2) # only used for the demo
driver.find_element(By.XPATH, "//*[contains(@id,'amazons-choice-label')]").click()
time.sleep(2) # only used for the demo
assert "UV500 PROTECTION" in driver.page_source
driver.quit()
--
Video Transcript
--
Видео How To: XPath Contains Function (2 Min) In Selenium Using Python | In PyTest & PyCharm автора Уборка профи
Видео How To: XPath Contains Function (2 Min) In Selenium Using Python | In PyTest & PyCharm автора Уборка профи
Информация
18 ноября 2023 г. 7:44:55
00:02:45
Похожие видео