1

I am using Selenium to scrape results from an online database. However, once I have selected my results and go to export them a pop-up window appears. Normally I would just switch the frame to this window but it appears to be javascript generated. While there are elements on this pop up, the specific name keeps changing. So for instance there is a submitButton id tag. However, it is alway appended with a different ending. So for instance it will be id="submitButton_15bb0b69431" but the next time it will be something completely different such as id="submitButton_15bb0c03e8e". My normal method of finding by ID or CSS or XPATH, therefore, does not work.

How can I handle this window to select the elements I need? Could I perhaps somehow get the page source for the popup?

The code I have so far is as follows:

for i in listofpa:
try:
    driver.find_element_by_xpath(i).click()

    driver.find_element_by_css_selector('#mainContentRight > div.pagination > ul > li:nth-child(13) > a').click()

except:
    pass

This is the part I am stuck on

time.sleep(5)
window_before = driver.window_handles[0]
driver.find_element_by_id("tsMore").click()
try:
    driver.find_element_by_css_selector('#saveExportLink_6').click()
    window_after = driver.window_handles[1]
    driver.switch_to_window(window_after)
    html = driver.page_source()
    print html

3 Answers 3

1

Use a wildcard in your css selector to get the modal dialog:

driver.find_element_by_css_selector('[id^="submitButton"]')

This will find the first element that has an id starting with submitButton

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

Comments

0

In xpath I think you can try //*[contains(@id,'submitButton_')] or //*[starts-with(@id,'submitButton_')] to locate the element.

Comments

0

Looks like you want to find by a partial match. In XPath you can use regular expressions. In CSS selectors you can match by prefix.

In Java this would be something like (from memory, not tested):

driver.FindElement(By.CssSelector("button[id^='submitButton']"))

More details at this SO question. Finding an element by partial id with Selenium in C#

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.