How to use to_be_visible method in Playwright Python

Best Python code snippet using playwright-python

selenium_scraper.py

Source:selenium_scraper.py Github

copy

Full Screen

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):...

Full Screen

Full Screen

test_github_project.py

Source:test_github_project.py Github

copy

Full Screen

...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()...

Full Screen

Full Screen

test_scroll_state.py

Source:test_scroll_state.py Github

copy

Full Screen

...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() ...

Full Screen

Full Screen

test_tables.py

Source:test_tables.py Github

copy

Full Screen

...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()...

Full Screen

Full Screen

Playwright tutorial

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.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful