How to make selenium wait before getting contents from the actual website which loads after the landing page through IEDriverServer and IE

人盡茶涼 提交于 2021-02-04 08:21:44

问题


driver = webdriver.Ie("C:\\IEDriverServer.exe")
driver.get(testurl)
driver.refresh()
time.sleep(5)
data = driver.find_element_by_id("__content0-value-scr")

So I'm trying to find an element by it's id using Selenium (Python) and Internet Explorer, because I'm limited to Internet Explorer due to company regulations.

My problem is as follows: on driver.get(testurl), selenium loads the page but IE first starts up with the IEDriver landing page. Only after that, it loads the requested url.

The problem here is that Selenium recognizes the IE Driver landing page as the url to be loaded and therefore ignores the page I want to search on, which gets loaded after that.

Has anyone got an idea on how to work around this?


回答1:


When you use Selenium, IEDriverServer and Internet Explorer, while IEDriverServer initiates a new IE Browser session, IE Browser first starts up with the IEDriver landing page and then loads the requested url.

Incase Selenium recognizes the IEDriverServer's landing page as the url to be loaded, in that case the solution would be to induce WebDriverWait for the Page Title to be equivalent to the actual page title of the AUT (Application Under Test).

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    testurl = "https://www.facebook.com/" # replace with the url of the AUT
    driver = webdriver.Ie(executable_path=r'C:\path\to\IEDriverServer.exe')
    driver.get(testurl)
    WebDriverWait(driver, 10).until(EC.title_contains("Facebook")) # # replace with the title of the AUT
    data = driver.find_element_by_id("__content0-value-scr")
    


来源:https://stackoverflow.com/questions/53438182/how-to-make-selenium-wait-before-getting-contents-from-the-actual-website-which

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