Python/Selenium - Getting the element after an element

帅比萌擦擦* 提交于 2021-01-04 07:33:05

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/49054985/python-selenium-getting-the-element-after-an-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!