0

I'm trying to find a way to click on "X" button but I cannot find the way to click on this button.

Element copy:

<div style="cursor: pointer; float:right; border-radius: 3px; background-color: red; font-size: 10px; color: white; height: 15px; width:15px; line-height: 15px;" onclick="fecharModal();">X</div>

Xpath:

//*[@id="meu_modal"]/div

Css Selector:

#meu_modal > div

Tried:

driver.find_element_by_css_selector("a[onclick*=fecharModal]").click();

Imports:

from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.action_chains import ActionChains import timeenter code here

7
  • 1
    have you tried driver.find_element_by_xpath("//*[@id="meu_modal"]/div").click() Commented Sep 23, 2021 at 2:24
  • Hi @Huzaifa, yes, I had tried your suggestion and the error - "NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='meu_modal']/div"} (Session info: chrome=93.0.4577.82) Commented Sep 23, 2021 at 3:13
  • I also had tried. #tried1: WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='meu_modal']"))).click() #tried2: WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@name='x']"))).click() #tried3: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='onclick']"))).click() Commented Sep 23, 2021 at 3:14
  • Does the Element get highlighted when you put that xpath in the DOM. If yes, check if the element is in an iframe. Commented Sep 23, 2021 at 3:48
  • The markup is a div, but you tried to select an anchor tag. Commented Sep 23, 2021 at 4:08

2 Answers 2

1

This seems to be an angular bases application, mostly WebDriverWait should do the job, but still below solution illustarte all the ways in Selenium to click a web element. Code trial 2 is more preferable and best practices.

I will use this xpath

//div[@onclick='fecharModal();' and text()='X']

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//div[@onclick='fecharModal();' and text()='X']").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@onclick='fecharModal();' and text()='X']"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@onclick='fecharModal();' and text()='X']")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@onclick='fecharModal();' and text()='X']")
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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Cruisepandey, thank you for your time to slipt out this explanations. In fact still didn't work. Code1: NoSuchElementException Code2: "TimeoutException: Message: " Code3: NoSuchElementException Code4: NoSuchElementException I had followed the "Step to check" that you had mentioned about xpath and the result get highlighted 1/1. I think that is almost there. haha
@user16792636 : I am pretty much sure either it is in iframe or not in selenium view port. Please check for iframe, and let me know if it is in iframe
0

Presuming that the xpath you provided is accurate, I suggest you try this code possibly this might work for you.

yourEle = driver.find_element_by_xpath("//*[@id='meu_modal']/div")
driver.execute_script("arguments[0].click();", yourEle)

2 Comments

And if needed add some wait or Thread sleep.
Hi @enthusiasticCoder, I had tried your suggestion but unfortunately . didn't work. message error: "NoSuchElementException: Message: no such element: Unable to locate element:"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.