I have the most bizarre problem with my Selenium test. I have the following classes:
class DataStorageTestSuiteChrome(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="chrome_driver_path")
self.driver.maximize_window()
self.driver.get("www.data_storage_website.com")
self.page = DataStoragePage(self.driver)
def tearDown(self):
self.driver.quit()
def test_sn_validation(self):
self.page.sn_element = 20001234567 # This line takes 2.5min to execute in Linux
self.assertFalse(self.page.is_valid_sn())
self.page.sn_element = self.ID_GENERATOR.sn
self.assertTrue(self.page.is_valid_sn())
class DataStoragePage:
'''Page Object for DataStorage page'''
sn_element = SnElement()
def __init__(self, driver):
self.driver = driver
class SnElement:
'''This class gets the search text from the specified locator'''
locator = "some_working_locator"
def __set__(self, obj, value):
"""Sets the text to the value supplied"""
driver = obj.driver
WebDriverWait(driver, 100).until(
lambda driver: driver.find_element(By.XPATH, self.locator))
driver.find_element(By.XPATH, self.locator).clear()
driver.find_element(By.XPATH, self.locator).send_keys(value) # This line takes 2.5min to execute in Linux
Now, the problem is that test case "test_sn_validation" takes a seconds to execute in Windows (both with headless mode on&off), and it takes 2.5 minutes in WSL2 Ubuntu. This is obviously quite perplexing to me, as I expected tests like that to run a lot faster on Linux.
I have tracked the reason for the slow behaviour down to driver.find_element(By.XPATH, self.locator).send_keys(value)
in SnElement class, due to send_keys() method. Moreover, that delay occurs only on the first run of send_keys() - every subsequent execution I ran via PDB was instantenous.
Has anyone seen a similar behaviour in WSL2 for send_keys(), and can recommend a solution?
In short, it's "typing" is slow, there's a workaround to paste from clipboard. As for configuring typing speed, can't say much.
The workaround goes something like this:
import pyperclip
from selenium.webdriver.common.keys import Keys
pyperclip.copy('foo')
element.send_keys(Keys.CONTROL, 'v')
I hope this will help!
I think it is not related to Linux. Two possible reasons:
1 Your some_working_locator
is not really working. However, I am not sure why if fails to be located after 150 seconds, not after 150.
2 Your wait is incorrect. The appropriate way is:
WebDriverWait(driver, 100).until(
lambda driver: driver.find_element_by_xpath(self.locator))
Note, that locator should be string.
3 try using:
wait.until(EC.element_to_be_clickable((By.XPATH, 'self.locator')))
or
wait.until(EC.visibility_of_element_located((By.XPATH, 'self.locator')))
instead. Check this answer https://stackoverflow.com/a/41873287/12730112
You also probably have implicit wait in your project, it may be the reason for bigger waiting times. Do not set waits to be so long. 30 seconds is enough for 99% of cases.
4 One more note: Change last rows of SnElement to
driver.find_element_by_xpath(self.locator).clear()
driver.find_element_by_xpath(self.locator).send_keys(value)
and check if it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With