Search code examples
python-3.xselenium-webdriverxpathiframeselenium-chromedriver

Select shelly using Python + Selenium


So I am new to using Selenium with Python. I would like to access a xpath on a specific website. To access this website one must first log in with an account, so my problem is not really reproduceable by outsiders.

I read something about switching to iframe before trying to reach certain xpaths but this website uses something else instead of iframe.

I want to access the xpath of this search field, on the right side we can see this 'shelly' thing (see first picture).

Website in dev mode

In the second picture we can see the xpath of the element I want to reach, but when I try to use it with Python/Selenium it won't get recognized...

xpath

I tried two xpaths (both won't work):

//[@id="puik-search-bar"] and /html/body/shelly-app/main/div1/section/app-root/app-search-page/div/div/div/div/app-search-product/div/puik-card/div/puik-search-bar//div/div2/input//

I also tried something like this:

driver.switch_to.frame(driver.find_element_by_class_name("shelly-app__page")) driver.switch_to.frame(driver.find_element_by_tag_name("shelly-app")) driver.switch_to.frame(1)

piece of code:

name =driver.find_element('xpath','//*[@id="puik-search-bar"]')
name.send_keys(sname)

The log in page to access this search bar can be automated with the standard selenium functions but after the log in page (the page with the search bar) the standard functions won't work


Solution

  • Instead of using selenium, I recommend using playwright. Playwright is one of the newer python libraries that is great for web browser automation.

    Install playwright like this: pip install playwright,

    and install browsers: playwright install. When you run this command playwright will start downloading chromium, webkit, and firefox. You still can, however, use chrome and msedge, you just need to run it on a channel.

    For example:

    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto('https://example.com')
        page.locator('input#puik-search-bar').fill('text')
        browser.close()
    

    If you want to launch webkit or firefox, simply replace chromium with the desired name in this line: browser = p.chromium.launch(). If you want to run chrome or microsoft edge, simply add a channel argument: browser = p.chromium.launch(channel="chrome"), but don't change chromium to msedge or chrome, and it will launch the desired browser.

    Note that playwright launches a browser in headless mode by default, so you'll have to add the headless=False argument to be able to see the browser opening (mainly for testing).

    Playwright allows use of both xpath and CSS, in this case, I used CSS: page.locator('input#puik-search-bar').fill('text'), but you could also use xpath if you wanted to, and playwright will recognize it automatically.

    The reason I suggest that you use playwright is because selenium is pretty old, and there have been several problems with webdrivers, finding text fields, browsers crashing, and more. Playwright is newer and is a much better choice for browser automation, and supports multiple different languages other than python.

    Playwright Documentation: https://playwright.dev/python/docs/intro