Automation Testing with selenium Click doesn't works on new Safari 17 IOS Sonoma 14.1

311 views Asked by At

everyone. I would like to expose my problem. My tests started to fail on Safari since I did upgrade to Safari 17, IOS Sonoma 14.1. In particular, is the click event of an element for example. Element.click() or button.click()

 elem =  web_driver.find_element(By.XPATH,Mylocator)          
 self.mouse_over(elem)
 elem.click()

it seems that the click event is not released, only by doing the click with JS, I can get it to work.

web_driver.execute_script("arguments[0].click();", elem)

I want to point out that the button is visible and not hidden from some other elements and I have no problems with other browsers (Chrome, Firefox, Edge). I had no problems even with Safari before the upgrade, everything worked fine.

Has anyone had my problem? if yes did you solve it? I would not like to use js all the time to perform my click tests. Any info is important thank you.

IOS Sonoma 14.1
Safari 17.1
Selenium 4.14.0

Update--- I found that the problem occurs when on the machine where the test runs some other application is open, iTerm or ActivityMonitor some alert, etc... quitting the applications the test works normally. If the safari window goes in the background the test fails, or rather the click is not released the item is not found

P.s The same tests are pass without any errors in Chrome, Firefox and Edge

3

There are 3 answers

1
DerpyCoder On

Try executing javascript click event Try executing focus event Try checking for any overlapping elements Ensure all of the elements have loaded in before executing your selenium script.

0
Abdalrahman On

An example of a problem you might be dealing with might be related to how Safari processors handle click events. With Element, you just go to the "click" of the element. click() or button. Click click () so. Selenium sends a native click event to the current element and then the button is clicked. However, situations can occur if the browser does not capture the event handle or there is no event registered.

One solution to solve the challenge is to use ActionChains from the Selenium library. This class allows us to take an element perform operations and process it in a chain. In your case, you can move the mouse over the element using the mouse_over() method and then use the click() method to click the element. Here's how you can do it:

Import ActionChains from selenium.webdriver.common.action_chains

elem = web_driver.find_object(By.XPATH, Mylocator);

actions = ActionChains ( web_driver ) 1.1 .
actions.move_element(elem) 1.1.
actions.click() .
Actions.performance()

Regarding the error in the in-flight click action when the window with the Safari browser is outside, this could be a Safari error. However, take advantage of the situation by maintaining background focus throughout the tests. You can do this by using the window_handles property of the WebDriver object to switch to the main window before executing the click action: You can do this by using the window_handles property of the WebDriver object to switch to the main window before executing it.

0
Michael Mintz On

If you're still having issues, you may want to try the Python Selenium framework, SeleniumBase, which has special code for handling all kinds of issues on various browsers. Set browser="safari" in the SB() init call to use Safari. Works on the system you described:

from seleniumbase import SB

with SB(browser="safari") as sb:
    sb.open("https://www.saucedemo.com/")
    sb.type("input#user-name", "standard_user")
    sb.type("input#password", "secret_sauce")
    sb.click("input#login-button")
    sb.select_option_by_text("select.product_sort_container", "Name (Z to A)")
    sb.click('button[id="add-to-cart-test.allthethings()-t-shirt-(red)"]')
    sb.click("div#shopping_cart_container a")
    sb.assert_element('span:contains("Your Cart")')
    sb.assert_text("Test.allTheThings() T-Shirt", "div.inventory_item_name")
    sb.assert_exact_text("1", "div.cart_quantity")
    sb.click("button#checkout")
    sb.assert_element('span:contains("Checkout: Your Information")')
    sb.assert_element("button#cancel")
    sb.type("input#first-name", "SeleniumBase")
    sb.type("input#last-name", "Rocks")
    sb.type("input#postal-code", "01720")
    sb.click("input#continue")
    sb.assert_element('span:contains("Checkout: Overview")')
    sb.assert_text("Test.allTheThings() T-Shirt", "div.inventory_item_name")
    sb.assert_text("$15.99", "div.inventory_item_price")
    sb.assert_exact_text("1", "div.cart_quantity")
    sb.click("button#finish")
    sb.assert_exact_text("Thank you for your order!", "h2")

That's a full test for a demo e-commerce website. After pip install seleniumbase, you can run the script with python. There's a lot more methods if needed, and options you can set.