5

I need to close a popup window from the website itself by pressing the "X" window on the top right. Here is the shortened, relevant part of my code:

chromedriver = r'C:\Users\do\Desktop\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
url = 'Fake.com'

browser.get(url) 
browser.find_element_by_id('imgAttachmentsImg').click()
# I need to close out the window after this

The issue is that there are no unique identifiers for the "X" button itself. However, the pop up itself does have a unique identifier. I just need to be able to flow it down to the X button.

Info from the page:

1. V<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="attachmentsDialogOverview" aria-labelledby="ui-id-3" style="position: absolute; height: auto; width: auto; top: 239px; left: 102px; display: block;">
  2. <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
    3. <span id="ui-id-3" class="ui-dialog-title">Attachments</span>
   4. V<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close">
       5. <span class="ui-button-icon-primary ui-icon ui-icon-closethick"</span>
       6. <span class="ui-button-text">close</span></button>

I am new to using Python and Selenium. I switched over from VBA and still don't exactly understand all the syntax yet, so I do make mistakes! I did find a bandaid solution by just having sendkeys press Escape. But I am trying to actually understand how to actually solve this. I am not sure what errors I made with my solution:

browser.find_element_by_xpath("//span[@id, 'ui-id-3']/following-sibling::button").click()

Questions

  1. Why did my solution not work?

  2. How would I locate "ui-id-3"(on line 3) and get to the element on Line 4 to click it?

  3. How would I locate "ui-id-3"(on line 3) and get to the element on Line 5 to click it? (just so I know how to move across multiple elements)


Relevant links I looked over:

Following-Sibling Python Selenium

Find next sibling element in Python Selenium?

Using XPath Selector 'following-sibling::text()' in Selenium (Python)

Python + Selenium WebDriver - Get div value if sibling contains string

What is the correct syntax for using a variable and following-sibling in Python Selenium?

7
  • I doubt if this popup is in other frame, check for frame switching. Commented Mar 1, 2018 at 17:15
  • It is in the same frame. I switched over after I figured it out. I can interact with the other elements in the pop up right now (finally). Commented Mar 1, 2018 at 17:19
  • so, what sort of error do you get when you attempt to click on the close button? Commented Mar 1, 2018 at 17:20
  • If I attempt to use "browser.find_element_by_xpath("//span[@id, 'ui-id-3']/following-sibling::button").click()", I get the following: "Message: invalid selector: Unable to locate an element with the xpath expression //span[@id, 'ui-id-3']/following-sibling::button because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[@id, 'ui-id-3']/following-sibling::button' is not a valid XPath expression." Commented Mar 1, 2018 at 17:29
  • So, you can try that xpath in your browser dev tool and see that it doesn't work. Try //span[@id='ui-id-3']/following-sibling::button Commented Mar 1, 2018 at 17:33

2 Answers 2

4

The xpath has a problem. this should work:

browser.find_element_by_xpath("//span[@id='ui-id-3']/following-sibling::button").click()

To get to the span below it, you could use:

browser.find_element_by_xpath("//span[@id='ui-id-3']/following-sibling::button/span")

by the way, if you're more comfortable with CSS, the equivalent paths would be:

div#ui-id-3 > button
div#ui-id-3 > button span:nth-of-type(1)
Sign up to request clarification or add additional context in comments.

2 Comments

THANK YOU. It worked and you explained everything(and more). The css addition is really nice too!. Just so I understand the CSS portion it would be something like this right...browser.find_element_by_css_selector("div#ui-id-3 > button span:nth-of-type(1)").click(). (Pretty sure that this is wrong)
actually, that ought to work...just that clicking on that span may not do much if there's no click event attached to the element.
0

You could also do something like this using the find_element() function:

##select an element
header = driver.find_elements(By.XPATH, '//h5')[0]
## get the next link
header.find_element(By.XPATH, './/following-sibling::a').get_attribute('outerHTML')

Comments

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.