0

I want to Automate Fill Form using Playwright Python version. On Playwright NodeJS, I've been successfully to do the desired. Only when Porting the JS Script into Playwright Python API, it's kinda different.

The Web has Multiple Button (Submit and Random) with different Name. I can achieve both Button via get_by_role but I have no idea to get into Specific Submit Button:

#HTML
<form method="post" action="?query=MyAPI" enctype="multipart/form-data">
            <table style="width: 100%">
                <tbody><tr>
                    <td style="width: 146px">URL:</td>
                    <td><input name="url" type="text" style="width: 100%"></td></tr>

                    <tr><td style="width: 146px">&nbsp;</td>
                    <td><br>
                        <input name="Random" type="random" value="Random" style="width: 97px">
                        <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <input name="Submit" type="submit" value="Submit" style="width: 157px">
                            &nbsp; </span>
                    </td></tr>
            </tbody></table>
        </form>

#JS Version
await page.getByRole('button', { name: 'Submit' }).click();

It seems the Python Version don't have { name: 'Submit'} feature, so I can only do ('button'). I'm not sure if I left something from the API, Already tried the following things but to not avail.

page.get_by_role('button').get_by_text('Submit').click()
page.get_by_role('button', {'name': 'Submit'}).click()

EDIT2 It's a Public UR, Here's The Submit Button XPath: /html/body/div[1]/div[2]/div[2]/article[1]/form/table/tbody/tr[2]/td[2]/span/input

EDIT1: The Button is a part of Multipart Forms, it has no ID so I can't find any Alternative solution. Button Image

8
  • name="Submit1" is correct or should be name="Submit"? Commented Dec 19, 2022 at 13:22
  • Ah that's the typo on my part, it's should be "Submit" @JakyRuby Commented Dec 19, 2022 at 13:24
  • Okay, you have an answer, I would say it is correct Commented Dec 19, 2022 at 13:25
  • Okay, looks like is something different. Is this an accessible url? Or only accessible from your work? Can you post a screenshot of the button? Is the button present or have to do any kind of scroll or something? If you open the dev tool in your browser and try to locate the xpath //input[@type="Submit"] how many results do you have? Commented Dec 19, 2022 at 13:34
  • Thread Updated.. @JakyRuby Commented Dec 19, 2022 at 13:36

1 Answer 1

1

Try with:

await page.locator('//input[@value="Submit"]').click();

Or

await page.locator('//input[@type="submit"]').click();

Or

await page.locator('//input[@name="Submit1"]').click();

Then you are trying with Xpath, which should be enough if that is the only submit value/name/type of the page.

Basically with .locator you are locating the element and then with .click() you are making click on that element previously located

Fully working example

from playwright.sync_api import sync_playwright
import time

def run(playwright):
    chrome = playwright.chromium
    browser = chrome.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://suip.biz/?act=iscloudflare")
    page.locator("//input[@name='url']").fill("google.com")
    page.locator("//input[@value='Submit']").click()
    time.sleep(10)
    browser.close()

with sync_playwright() as playwright:
    run(playwright)
Sign up to request clarification or add additional context in comments.

6 Comments

Hi! Trying both "Submit" and Submit always result in: waiting for locator("xpath=//button[@value=\"Submit\"]") and waiting for locator("xpath=//button[@value=Submit]") and the script Ends.
Answer updated, it is not a button, it is a input, try again please
The page still the same, it's doesn't click the submit button. Have been tried on different web subdomain. I'll try to Update the Thread for clearance.
@Xavi we missunderstand each other and page confused us. Value is Submit, name is Submit1 and type is submit. So we have a mix between capital letters, lowecase and also a char 1. Well, now it should work
I'm heavily sorry to bother again, printing the HTML (print(page.content())) still indicate the submit button hasn't clicked: imgur.com/a/Emz2ttY
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.