Home > Enterprise >  How to find which element is clicked in a webpage when the element is not a button and non-locatable
How to find which element is clicked in a webpage when the element is not a button and non-locatable

Time:12-21

I'm trying to automate a process using Selenium and Python. Using inspect in Chrome, I located the "button" (which is a span element):

<span data-columnnum="1" data-ispersonalareaviewable="1" data-
originalmfpriority="1" data- personalareaviewposition="manage_area" data-isrequired="0" name="editOrderBtn" id="editOrderBtn"  data-defaultvalue="" data-showonload="1" 
data- fieldid="9536" data-link="//my.yad2.co.il/newOrder/index.php?
action=edit&amp;CatID=2&amp;SubCatID=1&amp;OrderID=45330517" data-viewcommandactive="1" data- originalelementname="input">  <i ></i> <span >Edit Details</span> </span>

This element is inside a div. No button element anywhere in sight. I tried to find this element using Python and Selenium by name, path, etc. I can't find any trace of it. If I find it, I hope it is possible to us .click() on it, since it is not a button.

I will appreciated your help.

Trying to find it manually:

from selenium import webdriver
session=webdriver.Chrome(r'C:\Users\me\chromedriver')
session.get("https://my.yad2.co.il/newOrder/index.php?action=personalAreaFeed&CatID=2&SubCatID=1") # This is user-password protected and won't be accessible

for x in session.find_elements_by_xpath('//span'):
    print(x.get_property('attributes'))

Trying to use the name / id tags:

session.find_elements_by_id('editOrderBtn')
session.find_elements_by_name('editOrderBtn')

These two command results in an empty list.

  • Update: the element is non-locatable due to its being inside an iframe *

CodePudding user response:

If you are looking to click on Edit Details

the you can use the text-based locator xpath like below:

//span[text()='Edit Details']

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 is unique then there are 4 ways to perform click on it using Selenium.

Code trial 1:

time.sleep(5)
driver.find_element_by_xpath("//span[text()='Edit Details']").click()

Code trial 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Edit Details']"))).click()

Code trial 3:

time.sleep(5)
button = driver.find_element_by_xpath("//span[text()='Edit Details']")
driver.execute_script("arguments[0].click();", button)

Code trial 4:

time.sleep(5)
button = driver.find_element_by_xpath("//span[text()='Edit Details']")
ActionChains(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
  • Related