Best Python code snippet using playwright-python
selenium_scraper.py
Source: selenium_scraper.py
1import os2import sys3from selenium import webdriver4from selenium.webdriver.common.desired_capabilities import DesiredCapabilities5from selenium.webdriver.common.by import By6from selenium.webdriver.common.keys import Keys7from selenium.webdriver.support.ui import WebDriverWait8import selenium.webdriver.support.expected_conditions as EC9from selenium.common import exceptions as e10class SeleniumScraper:11 def __init__(self, url, search_query=None, search_id=None, wait_for_element=None, element_to_find=None, find_by_class=False, find_by_id=False):12 self.url = url13 self.search_query = search_query14 self.search_id = search_id15 self.wait_for_element = wait_for_element16 self.element_to_find = element_to_find17 self.find_by_class = find_by_class18 self.find_by_id = find_by_id19 self.chrome_options = webdriver.ChromeOptions()20 # When uploading to lambda, these need to be changed to /opt/chrome/..21 self.binary_path = os.getenv("binary_path")22 self.executable = os.getenv("executable")23 sys.path.append(self.executable)24 self.chrome_options.add_argument('--headless')25 self.chrome_options.add_argument('--no-sandbox')26 # self.chrome_options.add_argument('--disable-gpu')27 self.chrome_options.add_argument('--disable-dev-shm-usage')28 self.chrome_options.add_argument('--start-maximized')29 self.chrome_options.add_argument('--window-size=1920, 1080')30 self.chrome_options.add_argument('--hide-scrollbars')31 self.chrome_options.add_argument('--enable-logging')32 self.chrome_options.add_argument('--log-level=0')33 self.chrome_options.add_argument('--v=99')34 self.chrome_options.add_argument('--single-process')35 self.chrome_options.add_argument('--data-path=/tmp/data-path')36 self.chrome_options.add_argument('--ignore-certificate-errors')37 # self.chrome_options.add_argument('--homedir=/tmp')38 # self.chrome_options.add_argument('--disk-cache-dir=/tmp/cache-dir')39 # self.chrome_options.add_argument(40 # 'user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')41 if "local_remote" not in os.environ:42 os.environ["local_remote"] = "local"43 if os.getenv("local_remote") == "local":44 self.chrome_options.binary_location = self.binary_path45 self.webdriver = webdriver.Chrome(executable_path=self.executable, options=self.chrome_options)46 elif os.getenv("local_remote") == "remote":47 self.grid_url = f"http://{os.getenv('remote_url')}:4444/wd/hub"48 self.webdriver = webdriver.Remote(command_executor=self.grid_url, options=self.chrome_options)49 # retrive url in headless browser50 self.webdriver.set_page_load_timeout(90)51 try:52 self.webdriver.get(self.url)53 except e.TimeoutException as msg:54 self.webdriver.quit()55 raise e.TimeoutException(msg=msg)56 self.wait = WebDriverWait(self.webdriver, 8)57 def get_one_element(self):58 self.wait_element_appear(self.wait_for_element)59 if self.find_by_class:60 # element_to_find variable here is the class name or ID or xpath that is seen in the html inspection61 return self.webdriver.find_element_by_class_name(62 self.element_to_find)63 elif self.find_by_id:64 return self.webdriver.find_element_by_id(65 self.element_to_find)66 else:67 raise Exception("Specify if to find element by class or Id")68 def list_elements_on_page(self):69 # find search box70 if self.search_query is not None and self.search_query != "":71 search = self.webdriver.find_element_by_id(self.search_id)72 search.send_keys(self.search_query + Keys.RETURN)73 self.wait_for_element = "quotesList"74 self.wait_element_appear(self.wait_for_element)75 if self.find_by_class:76 # element_to_find variable here is the class name or ID or xpath that is seen in the html inspection77 yield self.webdriver.find_elements_by_class_name(78 self.element_to_find)79 elif self.find_by_id:80 yield self.webdriver.find_elements_by_id(81 self.element_to_find)82 else:83 raise Exception("Specify if to find element by class or Id")84 def check_if_displayed(self, element):85 element_to_check = self.webdriver.find_element_by_id(element)86 return element_to_check.is_displayed()87 def list_elements_by_tag(self, page_data=None):88 if page_data is None:89 self.wait_element_appear(self.wait_for_element)90 images = self.webdriver.find_elements_by_tag_name("img")91 else:92 images = page_data[0].find_elements_by_tag_name("img")93 yield images94 def wait_for_element_presence(self):95 try:96 self.wait.until(EC.presence_of_element_located(97 (By.ID, self.wait_for_element)))98 except e.TimeoutException:99 try:100 self.wait.until(EC.presence_of_element_located(101 (By.CLASS_NAME, self.wait_for_element)))102 except e.TimeoutException as te:103 print("Could not locate element using presence by id nor class")104 raise te105 def wait_element_appear(self, element, by_id=False, by_class=False, to_click=False, to_be_visible=False):106 if by_id or self.find_by_id:107 locator = By.ID108 elif by_class or self.find_by_class:109 locator = By.CLASS_NAME110 if to_click:111 self.wait.until(EC.element_to_be_clickable(112 (locator, element)))113 elif to_be_visible:114 self.wait.until(EC.visibility_of_element_located(115 (locator, element)))116 else:117 self.wait_for_element_presence()118 def wait_element_disappear(self, element, by_id=False, by_class=False):119 if by_id:120 locator = By.ID121 elif by_class:122 locator = By.CLASS_NAME123 self.wait.until(EC.invisibility_of_element_located(124 (locator, element)))125 def click_element_on_page(self, element_to_click, element_to_wait_on, by_id=False, by_class=False):126 if by_id:127 self.find_by_id = by_id128 elif by_class:129 self.find_by_class = by_class130 if self.find_by_id:131 self.wait_element_appear(132 element_to_click, by_id=True, to_click=True)133 self.webdriver.find_element_by_id(element_to_click).click()134 elif self.find_by_class:135 self.wait_element_appear(136 element_to_click, by_class=True, to_click=True)137 self.webdriver.find_element_by_class_name(element_to_click).click()138 else:139 raise Exception("Specify if to find element by class name or id")140 try:141 self.wait_element_appear(142 element_to_wait_on, by_id=True, to_be_visible=True)143 except e.TimeoutException:144 print("Could not find element: {}".format(element_to_wait_on))145 self.wait_element_disappear(element_to_wait_on, by_id=True)146 def stop_driver(self):...
test_github_project.py
Source: test_github_project.py
...54 # Load the project page55 page.goto(f'https://github.com/users/{gh_username}/projects/{gh_project["number"]}')56 # Verify the card appears in the first column57 card_xpath = f'//div[@id="column-cards-{source_col}"]//p[contains(text(), "{note}")]'58 expect(page.locator(card_xpath)).to_be_visible()59 # Move a card to the second column via web UI60 page.drag_and_drop(f'text="{note}"', f'id=column-cards-{dest_col}')61 # Verify the card is in the second column via UI62 card_xpath = f'//div[@id="column-cards-{dest_col}"]//p[contains(text(), "{note}")]'63 expect(page.locator(card_xpath)).to_be_visible()64 # Verify the backend is updated via API65 card_id = c_response.json()['id']66 r_response = gh_context.get(f'/projects/columns/cards/{card_id}')67 expect(r_response).to_be_ok()...
test_scroll_state.py
Source: test_scroll_state.py
...67def test_scroll_preserving_on_tabs_switching(page, go_to_patents_data_table):8 # initially, the 30th element is not in the view9 thirtieth_element = page.locator('span.number:text-is("30")')10 expect(thirtieth_element).not_to_be_visible()11 # scroll makes it visible12 page.locator(".ps__rail-y").click()13 expect(thirtieth_element).to_be_visible()14 # creating new table switches to its tab15 create_empty_table(page)16 get_table_entry(page, "patents").click()17 expect(thirtieth_element).to_be_visible()181920def test_scroll_preserving_on_active_table_updating(page, go_to_patents_data_table):21 thirtieth_element = page.locator("span.number:text-is('30')")22 expect(thirtieth_element).not_to_be_visible()23 page.locator(".ps__rail-y").click()24 expect(thirtieth_element).to_be_visible()25 rename_column(page, "Center", "Updated Center")26 expect(thirtieth_element).to_be_visible()272829# TODO: add 'Filter' when implemented30@pytest.mark.parametrize("action", ["Sort", "Group"])31def test_scroll_resetting_on_sort_group_applying(page, go_to_patents_data_table, action):32 thirtieth_element = page.locator("span.number:text-is('30')")33 expect(thirtieth_element).not_to_be_visible()34 page.locator(".ps__rail-y").click()35 expect(thirtieth_element).to_be_visible()36 page.locator(f"button:has-text('{action}')").click()37 page.locator(f"button:has-text('Add new {action} column')").click()38 page.locator("td.action >> button:first-child").click()39 expect(thirtieth_element).not_to_be_visible()404142def test_scroll_resetting_on_table_page_update(page, go_to_patents_data_table):43 thirtieth_element = page.locator("span.number:text-is('30')")44 expect(thirtieth_element).not_to_be_visible()45 page.locator(".ps__rail-y").click()46 expect(thirtieth_element).to_be_visible()47 page.locator("[aria-label='Goto Page 2']").click()48 expect(page.locator("span.number:text-is('501')")).to_be_visible()495051def test_scroll_resetting_on_table_page_size_update(page, go_to_patents_data_table):52 thirtieth_element = page.locator("span.number:text-is('30')")53 expect(thirtieth_element).not_to_be_visible()54 page.locator(".ps__rail-y").click()55 expect(thirtieth_element).to_be_visible()56 page.locator("button:has-text('500')").click()57 page.locator("li[role='option']:has-text('100')").click()
...
test_tables.py
Source: test_tables.py
...7 page.goto(base_schema_url)8 expect(get_tables_list(page)).to_be_empty()9 # Table 010 create_empty_table(page)11 expect(get_table_entry(page, "Table 0")).to_be_visible()12def test_delete_empty_table(page, base_schema_url):13 page.goto(base_schema_url)14 # No entry in sidebar15 expect(get_tables_list(page)).to_be_empty()16 # Create Table 017 create_empty_table(page)18 expect(get_table_entry(page, "Table 0")).to_be_visible()19 # Delete Table 020 delete_active_table(page)21 # No Table 0 entry in the sidebar and no tab22 expect(get_tables_list(page)).to_be_empty()23 expect(get_tab(page, "Table 0")).not_to_be_visible()24 expect_welcome_to_be_visible(page, welcome_text)25def test_rename_empty_table(page, base_schema_url):26 page.goto(base_schema_url)27 # Create Table 028 create_empty_table(page)29 table_0_entry = get_table_entry(page, "Table 0")30 table_0_tab = get_tab(page, "Table 0")31 rename_table(page, "Table 1")32 # Table 1 in the sidebar and in the tab title33 expect(get_table_entry(page, "Table 1")).to_be_visible()34 expect(get_tab(page, "Table 1")).to_be_visible()35 # Table 0 not visible36 expect(table_0_entry).not_to_be_visible()37 expect(table_0_tab).not_to_be_visible()38def test_rename_table_of_another_table(page, base_schema_url):39 page.goto(base_schema_url)40 expect(get_tables_list(page)).to_be_empty()41 page.click("[aria-label='New Table']")42 page.click("button:has-text('Empty Table')")43 expect(get_table_entry(page, "Table 0")).to_be_visible()44 page.click("[aria-label='New Table']")45 page.click("button:has-text('Empty Table')")46 expect(get_table_entry(page, "Table 1")).to_be_visible()47 page.click("[aria-label='Table']")48 page.click("text=Rename")49 page.press("[aria-label='name']", "ArrowRight")50 page.fill("[aria-label='name']", "Table 0")51 expect(page.locator("text=A table with that name already exists.")).to_be_visible()...
Playwright error connection refused in docker
playwright-python advanced setup
How to select an input according to a parent sibling label
Error when installing Microsoft Playwright
Trouble waiting for changes to complete that are triggered by Python Playwright `select_option`
Capturing and Storing Request Data Using Playwright for Python
Can Playwright be used to launch a browser instance
Trouble in Clicking on Log in Google Button of Pop Up Menu Playwright Python
Scrapy Playwright get date by clicking button
React locator example
I solved my problem. In fact my docker container (frontend) is called "app" which is also domain name of fronend application. My application is running locally on http. Chromium and geko drivers force httpS connection for some domain names one of which is "app". So i have to change name for my docker container wich contains frontend application.
Check out the latest blogs from LambdaTest on this topic:
The sky’s the limit (and even beyond that) when you want to run test automation. Technology has developed so much that you can reduce time and stay more productive than you used to 10 years ago. You needn’t put up with the limitations brought to you by Selenium if that’s your go-to automation testing tool. Instead, you can pick from various test automation frameworks and tools to write effective test cases and run them successfully.
When it comes to web automation testing, there are a number of frameworks like Selenium, Cypress, PlayWright, Puppeteer, etc., that make it to the ‘preferred list’ of frameworks. The choice of test automation framework depends on a range of parameters like type, complexity, scale, along with the framework expertise available within the team. However, it’s no surprise that Selenium is still the most preferred framework among developers and QAs.
Playwright is a framework that I’ve always heard great things about but never had a chance to pick up until earlier this year. And since then, it’s become one of my favorite test automation frameworks to use when building a new automation project. It’s easy to set up, feature-packed, and one of the fastest, most reliable frameworks I’ve worked with.
The speed at which tests are executed and the “dearth of smartness” in testing are the two major problems developers and testers encounter.
With the rapidly evolving technology due to its ever-increasing demand in today’s world, Digital Security has become a major concern for the Software Industry. There are various ways through which Digital Security can be achieved, Captcha being one of them.Captcha is easy for humans to solve but hard for “bots” and other malicious software to figure out. However, Captcha has always been tricky for the testers to automate, as many of them don’t know how to handle captcha in Selenium or using any other test automation framework.
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!