I am working on a selenium script in Python, where I am on this stage trying to locate a game icon.
But I can't locate it and click.
This is what I've tried:
self.driver.find_element_by_xpath('/html/body/div[5]/ul/li[17]/a/img')
self.driver.find_element_by_xpath("//*[@id="jackpot_01001"]")
self.driver.find_element_by_xpath('//*[@id="gameList_SLOT_01001"]')
self.driver.find_element_by_id("jackpot_01001").click()
But it will show:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/ul/li[17]/a/img"} (Session info: chrome=93.0.4577.82)
CodePudding user response:
This error
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/ul/li[17]/a/img"} (Session info: chrome=93.0.4577.82)
implies that Either one of the following :
- Element locator is not correct.
- Element is in iframe.
- Element is in shadow root.
I am not sure if //li[@id='gameList_SLOT_01001']
or /html/body/div[5]/ul/li[17]/a/img
is unique or not. You need to check in HTML DOM.
PS : Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
If it happens to be a unique node, then put some waits and then perform the click.
Update 1:
There are 4 ways to click in Selenium.
I will use this xpath
//li[@id='gameList_SLOT_01001']/descendant::img
Code trial 1:
time.sleep(5)
self.driver.find_element_by_xpath("//li[@id='gameList_SLOT_01001']/descendant::img").click()
Code trial 2:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@id='gameList_SLOT_01001']/descendant::img"))).click()
Code trial 3 :
time.sleep(5)
button = self.driver.find_element_by_xpath("//li[@id='gameList_SLOT_01001']/descendant::img")
self.driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = self.driver.find_element_by_xpath("//li[@id='gameList_SLOT_01001']/descendant::img")
ActionChains(self.driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
Update 2 :
Since it's a new tab, you would have to switch to it first before interacting with any web element on this new tab.
self.driver.find_element_by_id('RTGWEB-RTGWEB').click()
time.sleep(5)
self.driver.switch_to.window(driver.window_handles[1])
time.sleep(5)
self.driver.find_element_by_xpath("//li[@id='gameList_SLOT_01001']/descendant::img").click()