0
votes

enter image description hereI'm trying to create a macro that opens up all my online classes on Chrome (cause logging in is annoying, especially if you have to do it 8 times every morning).

BTW, I use Python 3.8, Chrome 81.0.4044.122, and the latest version of Selenium.

Until now, I clicked button using: driver = webdriver.Chrome() driver.find_element_by_xpath("PATH_OF_ELEMENT").click() And then I find a login button that has a image instead of text. I tried XPath, CSS Selector, id, name, the link of text, ActionChains (move_to) nothing works.

Here's the HTML: here click me please.

The button I'm trying to press is the one with the tag a.

I spent 30 minutes googling about this and all I found was Stack Overflow questions from 6 years ago. They suggested I use WebDriverWait or change the frame. None of them worked (I might have made a mistake). I'm new to Selenium so please be kind and explain hard stuff.

How can I find the correct XPath of an image button and click it?

driver.find_element_by_css_selector('.nice-select').click() driver.find_element_by_xpath("/html/body/div1/div[3]/div/div/div/div/div/div2/div1/div/ul/li2").click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/span').click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/ul/li[19]').click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/span').click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/ul/li[3]').click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/span').click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/ul/li[3]').click() driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[5]/a').click()

1
provide more info: example code, website etc.. we cannot guess what you have tried and what is actually the website implementation.Infern0
Thats not the point, I just need to be able to find the correct Xpath and click the image.Jooney
are you looking for this? //a/img[@src and @alt='mypicture']Infern0
I tried driver.find_element_by_css_selector('//a/img[@src and @alt=mypicture).click() It says selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified (Session info: chrome=81.0.4044.122)Jooney
Put your HTML in your question. Thanks.Ratmir Asanov

1 Answers

1
votes

Try the following CSS Selector:

.my_menu>a

Code should look like:

driver.find_element_by_css_selector(".my_menu>a").click()

Also, try to locate the element with explicit wait:

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


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".my_menu>a"))).click()

I have tested with the following code block (as result will be displayed popup with 2 options):

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


driver = webdriver.Chrome()
driver.get("https://oc31.ebssw.kr/onlineClass/search/onlineClassSearchView.do?schulCcode=00898&schCssTyp=online_mid")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
    (By.CSS_SELECTOR, ".my_menu>a"))).click()
time.sleep(5)

I hope it helps you!