简体   繁体   中英

Selenium WebDriver + PhantomJS + Python - Execute Script and Handle Dialog Box

I'm fairly new to coding, so forgive me if this is a dumb question:

I'm trying to get data from a list that is populated by executing a JavaScript, here's the relevant HTML section:

<a data-popover-ui="524151" data-popover-type="Search" href="javascript:LoadLists('SaveListSearchDetails.aspx?AutoLoad=1&amp;SavedListID=EID77FF7H&amp;SavedListTypeID=Search')" data-original-title="" title=""><span class="grid-result no-break">AVEV HRC FEC Master File</span></a>

After clicking this hyperlink, there's a dialog box that asks for confirmation

Here's what I tried:

browser = selenium.webdriver.PhantomJS(executable_path=r"E:\phantomjs-2.1.1-windows\bin\phantomjs.exe")

browser.get('~~link where the javascript is~~')

first_list = browser.execute_script("LoadLists('SaveListSearchDetails.aspx?AutoLoad=1&amp;SavedListID=EID77FF7H&amp;SavedListTypeID=Search')"

browser.get('~~link where the list is~~')

browser.get_screenshot_as_file

browser.save_screenshot('1.jpg')

The screenshot is there for me to see if it worked quickly. The list is not being updated. I have almost no clue about java coding, so I'm not sure at all about what's going on, but I think it's because when the JavaScript is being executed, I'm not dealing with the dialog box that shows up.

I guess I could use get_element_by_SOMETHING, but I'm not sure which to use, since there's no ID in that HTML section, and I still need help with handling the damn dialog box.

Thanks yoooo


PhantomJS was not working. Switched to Chrome as the WebDriver.

t_list = find_element_by_link_text("~~text~~")
t_list.click()
time.sleep(5)
Alert(browser).accept()

好吧,你可能需要在执行脚本之后或之后延迟,以便列表加载(假设它在普通浏览器中加载)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM