0

I have the below code

After coming back to main window unable to get focus and disconnect button is not getting clicked

parent_handle = driver.window_handles[0]

#clicks on  link
driver.find_element_by_xpath("//*[@id='content']/div[4]/div/p/a").click()

child_handle = [x for x in driver.window_handles if x != parent_handle][0]
driver.switch_to.window(child_handle)

time.sleep(2)

#will work on this winow

driver.close()

driver.switch_to.window(parent_handle)
time.sleep(30)

#click on Disconnect button on toolbar on main window
driver.find_element_by_xpath("//a[contains(text(), 'btn btn-outline-dark  et-disconnect-link et-disconnect-warning-link')]").click()

I tried several ways but getting the below error

selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"xpath","selector":"//a[contains(text(), 'btn btn-outline-dark  et-disconnect-link et-disconnect-warning-link')]"}
  (Session info: chrome=79.0.3945.117)
tripleee
  • 175,061
  • 34
  • 275
  • 318
Devi
  • 3
  • 3
  • Please review [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) – tripleee Jan 09 '20 at 07:50

2 Answers2

0

You didn't post the html, but the xpath looks like classes, not text

driver.find_element_by_xpath("//a[@class='btn btn-outline-dark et-disconnect-link et-disconnect-warning-link')]").click()

Or by css_selector

driver.find_element_by_css_selector(".btn.btn-outline-dark.et-disconnect-link.et-disconnect-warning-link").click()

You should also use explicit wait instead of time.sleep(30)

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(driver, 30)

driver.switch_to.window(parent_handle)
# time.sleep(30) -> remove this
element = wait.until(ec.visibility_of_element_located((By.XPATH, "//a[@class='btn btn-outline-dark  et-disconnect-link et-disconnect-warning-link')]")))
element.click()
Guy
  • 46,488
  • 10
  • 44
  • 88
0

Seems you were almost there. You need to consider a few more things as follows:

  • Assuming initially there is only one window_handle opened, instead of parent_handle = driver.window_handles[0] you can use driver.current_window_handle
  • Once you close the child window through driver.close(), you can use switch_to.default_content().
  • btn btn-outline-dark et-disconnect-link et-disconnect-warning-link seems to be the value of class attribute.
  • As your usecase is to invoke click() you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

    • Using CSS_SELECTOR:

      parent_handle = driver.current_window_handle
      #clicks on  link
      driver.find_element_by_xpath("//*[@id='content']/div[4]/div/p/a").click()
      child_handle = [x for x in driver.window_handles if x != parent_handle][0]
      driver.switch_to.window(child_handle)
      #will work on this winow
      driver.close()
      driver.switch_to.window(parent_handle)
      #click on Disconnect button on toolbar on main window
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-outline-dark..et-disconnect-link.et-disconnect-warning-link"))).click()
      
    • Using XPATH:

      parent_handle = driver.current_window_handle
      #clicks on  link
      driver.find_element_by_xpath("//*[@id='content']/div[4]/div/p/a").click()
      child_handle = [x for x in driver.window_handles if x != parent_handle][0]
      driver.switch_to.window(child_handle)
      #will work on this winow
      driver.close()
      driver.switch_to.window(parent_handle)
      #click on Disconnect button on toolbar on main window
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-outline-dark  et-disconnect-link et-disconnect-warning-link']"))).click()
      
    • Note : You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352