Giter VIP home page Giter VIP logo

Comments (7)

mxschmitt avatar mxschmitt commented on June 8, 2024 1

Its possible! You can set force=True:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto('https://codepen.io/kanpeki/pen/RLboWN')
    my_checkbox = page.frame_locator("iframe[name='CodePen']").locator("[id='slideThree']")
    my_checkbox.highlight()
    my_checkbox.set_checked(True, force=True)
    browser.close()

See here: https://playwright.dev/docs/actionability#forcing-actions

from playwright-python.

mxschmitt avatar mxschmitt commented on June 8, 2024

I tried the following which works for me:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto('http://example.com')
    page.set_content("""
        <label class="ui-switch">
            <input type="checkbox" id=":chb" tabindex="0" class="form-control">
            <span class="slider round">
                <div>
                ::before
                </div>
            </span>
        </label>
    """)
    my_checkbox = page.locator("[id=':chb']")
    my_checkbox.set_checked(False)
    browser.close()

from playwright-python.

mandras73 avatar mandras73 commented on June 8, 2024

Thank you Max, yes, this is strange... when setting the content it's working.
But when I'm using my live page, it doesn't...
Do you have any idea how could I debugging the root cause?
My slider is visible, enabled and nothing blocks it on the page... could that be a javascript (onchange event) related issue?
When adding .highlight() I can see the element has found... but the set_checked cannot be executed

from playwright-python.

mandras73 avatar mandras73 commented on June 8, 2024

Here is another example with the bug, please execute and check the results:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto('https://codepen.io/kanpeki/pen/RLboWN')
    my_checkbox = page.frame_locator("iframe[name='CodePen']").locator("[id='slideThree']")
    my_checkbox.highlight()
    my_checkbox.set_checked(True)
    browser.close()

from playwright-python.

mxschmitt avatar mxschmitt commented on June 8, 2024

Have you tried using Codegen to generate a locator? A user does not click in this scenario on the input element itself, the user instead clicks on the label. The input element itself is not visible in this case. So you can do something like this (generated by Codegen):

import re
from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://codepen.io/kanpeki/pen/RLboWN")
    page.frame_locator("iframe[name=\"CodePen\"]").get_by_title("Slide 3").locator("label").click()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

from playwright-python.

mandras73 avatar mandras73 commented on June 8, 2024

I would say, the fact that set_checked() cannot be used on hidden (or display: none) input, is an unexpected limitation.
Is it possible to remove the verification of the element visibility from playwright?
As you can see, we have scenarios like this, when input field is not visible but would like to control over the set_checked()

from playwright-python.

mandras73 avatar mandras73 commented on June 8, 2024

Thank you!

from playwright-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.