Trouble in Accepting the Dialog Box using Playwright Python - python

Hoe can I accept the dialog using python playwright. For your kind information I have already tried this code but it doesn't seems to work for me. Any other solution other than that will be appreciable. Thanks
from playwright.sync_api import sync_playwright
def handle_dialog(dialog):
print(dialog.message)
dialog.dismiss()
def run(playwright):
chromium = playwright.chromium
browser = chromium.launch()
page = browser.new_page()
page.on("dialog", handle_dialog)
page.evaluate("alert('1')")
browser.close()
with sync_playwright() as playwright:
run(playwright)

Related

Error generating PDF (blank or format error) - Playwright Python

Context:
Playwright Version: 1.29.1
Operating System: Windows
Python version: 3.8.2
Browser: Chromium
Describe the bug
This error happens in some specific situations, usually when directly or indirectly opening a pdf preview page.
def test():
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False) _**#In this situation, with headless set to False, the blank pdf is issued, but when set to True, it is not possible to send it. Any suggestion?**_
context = browser.new_context()
page = context.new_page()
page.goto('https://nyc3.digitaloceanspaces.com/midia/wp-content/uploads/2023/01/4tmrioel-sample.pdf')
page.pdf(path='Test.pdf', format='A4')
test()
For example, in the code snippet below, the pdf is generated empty, blank (the screenshots are black with no content). I noticed that this error happened because of the speed, so I used
the sleep library, as wait_for_load_state() does not work in this case.
However, the new generated pdfs come out with the wrong formatting (here is an attached image showing a print, I hid the content, but the layout is the same withou the black)
enter image description here
My theory is that the page generates like this because of the chromium pdf viewer summary. So, I tried to disable it in this code:
def test():
from playwright.sync_api import sync_playwright
from time import sleep
with sync_playwright() as p:
# browser = p.chromium.launch(headless=False)
browser = p.chromium.launch_persistent_context(user_data_dir=r'C:\Users\pedro\AppData\Local\Temp\playwright_chromiumdev_profile-AidV4Q\Default', args=['--print-to-pdf', '--disable-extensions', '--print-to-pdf-no-header'], headless=False)
page = browser.new_page()
page.goto('https://nyc3.digitaloceanspaces.com/midia/wp-content/uploads/2023/01/4tmrioel-sample.pdf')
sleep(5)
page.pdf(path='test.pdf', format='A4')
input()
Still, I couldn't solve the problem.
Details: Unable to run these codes in Headless mode, chromium appears to be being automated (making detection easier). So, does anyone have a solution to my problem?
You can't manage because it is not allowed in headless mode. As you can read in official docs: https://playwright.dev/python/docs/api/class-page#page-goto

Uploading an image to a website with Playwright

I'm trying to click the button upload an image to this website: https://prnt.sc/
But it seems like there is not even a [button], so can I even click anything? Is this even possible? Super confused.
There's lots of documentation on how to do this with selenium, but not much for Playwright unfortunately.
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, slow_mo=50)
page = browser.new_page()
page.goto("https://prnt.sc/")
page.locator("class=uploader__browse_button").click()
I am not using page.click because there is no button.
(From what I can see)
I still get errors using this code.
I've gone through the websites code and found
<form action="https://prntscr.com/upload.php" method="post" id="fileupload">
Hopefully that helps.
Just use set_input_files. Here is an example:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.webkit.launch()
page = browser.new_page()
page.goto('https://prnt.sc/')
# click on AGREE privacy
page.click('button[mode="primary"]')
# set file to form field
page.set_input_files('input[type="file"]', 'FULL_PATH_TO_FILE_HERE')
# wait for a link after upload
link = page.wait_for_selector('#link-textbox', state='visible').inner_text()
print(f'file link: {link}')
page.screenshot(path='example.png')
browser.close()

React locator example

I am trying to understand how the react selectors are working according to https://playwright.dev/docs/selectors#react-selectors . So I am trying some things in playwright sandbox. Seems that the react component cannot be found.
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://www.glassdoor.co.uk/Job/qa-engineer-jobs-SRCH_KO0,11.htm")
page.locator("_react=q[key='1007467366491']").click()
browser.close()
Error:
playwright._impl._api_types.TimeoutError: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for selector "_react=q[key='1007467366491']"
============================================================
sandbox example
Are there any more detailed examples for react out there?
Playwright does not support key filtering at the moment. But you can filter for the job.id which is part of the props:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://www.glassdoor.co.uk/Job/qa-engineer-jobs-SRCH_KO0,11.htm")
page.locator("_react=q[job.id=1007630619432]").click()
browser.close()

Function testing with playwright and python

I'm kinda new in coding and was asked to do a test for the company web login, they want me to implement the module unitestt and playwright test generator tool. This is what I have so far. I had to separate run from test_1 since unittest failed while reading the chromium line but now it only opens the browser so what can I do in order for it to run the whole test?
from playwright.sync_api import Playwright, sync_playwright
from locators import Locators_evou
import unittest
class Test(unittest.TestCase):
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(channel ="chrome", headless=False,slow_mo=500)
context = browser.new_context()
page = context.new_page()
page.goto("http://localhost:3000/")
def test_1(page):
page.click(Locators_evou.user_Log)
page.fill(Locators_evou.user_Log, "Liliana")
page.click(Locators_evou.password_log)
page.fill(Locators_evou.password_log, "1234")
page.check(Locators_evou.session_Log)
page.click(Locators_evou.login_log)
assert page.is_visible("¡Bienvenido!")
with sync_playwright() as playwright:
run(playwright)
if __name__=="__main__":
unittest.main()

send files to a website through a 'browse files' on pc

I'm browsing through a website using dryscrape in python and i need to upload a file to this site. But there is only one way of doing it, that is clicking in a button and browse into my files and select the one i want. How can i do it with python? i would appreciate if someone could help me using dryscrape too, but i'm accepting all answers.
heres the example image:
You can use Selenium. I tested this code and it works.
from selenium import webdriver
url = "https://example.com/"
driver = webdriver.Chrome("./chromedriver")
driver.get(url)
input_element = driver.find_element_by_css_selector("input[type=\"file\"]")
# absolute path to file
abs_file_path = "/Users/foo/Downloads/bar.png"
input_element.send_keys(abs_file_path)
sleep(5)
driver.quit()
Resources
Selenium python
Chrome driver download
For those who are searching for the answer in dryscrape i translated the selenium code to dryscrape:
element = sessin.at_xpath("xpath...") # Session is a dryscrape session
# The xpath is from the button like the one in the image "Browse..."
element.set("fullpath")
just as simple as it is.

Categories

Resources