0

There is a dropdown menu in which I need to click on the 2 item in list. So it works perfectly in Pycharm but not on server.

Code trials:

goods_count = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[6]/div[3]/div/div/div/div/main/div/div[*]/div[3]/div/div/div/ul/li[3]')))
driver.execute_script("arguments[0].click();", goods_count)`

What could be the difference between Pycharm and server side?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
anfele
  • 39
  • 6

1 Answers1

0

To click on any clickable element instead of presence_of_element_located() ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

goods_count = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[6]/div[3]/div/div/div/div/main/div/div/div[3]/div/div/div/ul/li[3]'))) 
driver.execute_script("arguments[0].click();", goods_count)

In a single line:

driver.execute_script("arguments[0].click();", WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[6]/div[3]/div/div/div/div/main/div/div/div[3]/div/div/div/ul/li[3]'))))

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Got error in Pycharm: path:filename.py:63: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome( Traceback (most recent call last): File "file.py", line 116, in goods_count = WebDriverWait(driver, 5).until(EC.element_to_be_clickable( File "path\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: Ordinal0 [0x00F27413+2389011] ... – anfele Apr 25 '22 at 14:07
  • Saw answers increasing wait value can helps - nope – anfele Apr 25 '22 at 14:08
  • There was an error in the xpath, corrected it. Please retest and let me know the ststus. – undetected Selenium Apr 25 '22 at 14:10
  • Error even with correct xpath File "path/file.py", line 105, in driver.execute_script("arguments[0].click();", WebDriverWait(driver, 10).until(EC.element_to_be_clickable( File "path\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – anfele Apr 25 '22 at 14:20
  • Doesn't the code in the answer work in _Pycharm_ as it worked earlier? – undetected Selenium Apr 25 '22 at 14:22
  • First, code worked in Pycharm but not on server. Now it doesn't even in Pycharm – anfele Apr 25 '22 at 14:26
  • See the embedded discussions for ` presence_of_element_located()` and `element_to_be_clickable()` within the answer. May be not the desired element. – undetected Selenium Apr 25 '22 at 14:28
  • A wild guess the `li` elements should be hidden and you need to expand the `
  • ` elements clicking on some other element.
  • – undetected Selenium Apr 25 '22 at 14:31