Home > database >  How to click on popup in Selenium?
How to click on popup in Selenium?

Time:10-18

I am using Selenium to automate a web task, and I am trying to search for an address. Once I search for an address, a popup will appear saying, "Did you mean _____ address," upon which I want to click on that popup.

enter image description here

You can see I've typed Stanford using Selenium, and I wish to click on the first result that pops up in that "Did you mean?" box. However, I'm having trouble getting that webbrowser element to click.

I am using Selenium Python.

I attempted using XPath helper chrome extension, which allowed me to determine that the XPath to that specific element is

/html/body[@class='vsc-initialized']/form[@id='Form1']/div[@class='stickywrapper']/div[@class='tier3']/table[@class='map-container']/tbody/tr/td/div[@class='css_container']/div[@id='m_upSearch']/div[@id='m_pnlSearchTab']/div[@id='m_pnlSearch']/div[@class='css_content'][1]/div[@id='m_sfcSearch']/div[@class='searchForm']/table/tbody/tr[2]/td/table/tbody/tr[2]/td[1]/table/tbody/tr[12]/td[2]/table/tbody/tr[2]/td[2]/div/div[@class='mapSearchDialog']/ul[@class='disambiguation']/li[2]

However, attempting driver.find_elements_by_xpath( . ) called on that string above did not work for me.

Any advice?

Thanks in advance!

CodePudding user response:

This could be cause element have not been rendered properly and you are trying to click on it/interact with it, resulting in exception.

Please induce explicit waits, which is Implemented by WebDriverWait.

The xpath is also brittle since it is absolute xpath. Please use a relative xpath like this :

//ul[@class='disambiguation']//li[addr]

Before using you should make sure that it should be reflected as 1/1 or 1/any_number. But it has to be 1 on the left side.

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.

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//ul[@class='disambiguation']//li[addr]").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='disambiguation']//li[addr]"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//ul[@class='disambiguation']//li[addr]")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//ul[@class='disambiguation']//li[addr]")
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

CodePudding user response:

I cannot see the complete HTML code, but check from your end if that is inside any frames? if yes then try to first switch your driver control to that frame and then perform that click action. Also, I would suggest that you should try to first capture all the WebElements from that list and then try to click on that second item from the given list by iteration so that in future if you would like to click on any other item you would be able to easily do it.

  • Related