How to click menu element in Wordpress admin panel using Selenium

64 views Asked by At

What Im doing:

My coworker send me video where I have to import 8 blocks for each non main page. Earlier I was working with selenium, and I thought could I automate this?

I have tried code that is code to log in to wordpress. Then I want to automate editing pages on elementor with importing blogs.

Right now Im stuck with clicking on wordpress menu that has only class identifier.

enter image description hereenter image description here

import time
from selenium import webdriver
from selenium.webdriver.safari.service import Service
#from selenium.webdriver.safari.options import SafariOptions
#from webdriver_manager.safari import SafariDriverManager

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


#options = SafariOptions()
#options.add_experimental

browser = webdriver.Safari()

browser.get('http://akaska.hledampneu.cz/wp-admin')

time.sleep(1)
Passname = browser.find_element(By.ID,"user_login")
#Passname = browser.find_element(by=id("user_login"))
Passname.send_keys('admin.akaska')

Password = browser.find_element(By.ID, "user_pass")
Password.send_keys('tdYFxncD9gbd6Q)Y*2')

time.sleep(1)
submit = browser.find_element(By.ID,"wp-submit").click()

#submit = browser.find_element(By.CLASS_NAME,"icon-options")

time.sleep(1)

#submit = browser.find_element(By.ID,"wp-admin-bar-site-name")

submit = browser.find_element(By.CSS_SELECTOR, 'span[class="ab-item"]').click()

What have I tried:

1.) CSS_SELECTOR

submit = browser.find_element(By.CSS_SELECTOR, 'span[class="ab-item"]').click()

This is the error that it came up with. When I tried to click only with class

Traceback (most recent call last):
  File "/Users/justin/Desktop/projects/wordpressAutomation /main.py", line 36, in <module>
    submit = browser.find_element(By.CSS_SELECTOR, 'span[class="ab-item"]').click()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 741, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: ; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

2.) CLASS_NAME

submit = browser.find_element(By.CLASS_NAME,"ab-item").click()
Traceback (most recent call last):
  File "/Users/justin/Desktop/projects/wordpressAutomation /main.py", line 34, in <module>
    submit = browser.find_element(By.CLASS_NAME,"ab-item").click()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 741, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: ; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
1

There are 1 answers

0
Yaroslavm On

Admin panel contains both elements for small browser resolutions and for maximized window.

First of all, there is no span elements with class [class="ab-item"], however there are a elements with current class.

To click on your element you need to filter your elements by visibility, as far as first element is element that is visible in mobile view, and if you try to click on it, it would be not visible, so not interactable.

elements = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'a[class="ab-item"]')))
[element for element in elements if element.is_displayed()][0].click()