“此页面无法加载”Selenium 按钮重定向链接未加载

问题描述 投票:0回答:1

我将 url 作为输入,并在搜索栏中输入一些文本,而不是单击第一个搜索建议并在 Python 中使用 selenium 单击搜索图标。

但是现在selenium驱动程序无法加载重定向的url。(此页面无法加载错误)。它属于同一域。当我自己在其他窗口中打开相同的链接时。它正在工作。

环境:

操作系统 => Mac arm-64,
驱动程序 =>ChromeDriver 稳定版
版本:129.0.6668.58(r1343869)

def extractSocietyData(societyName:str)->None:
    
    driver = webdriver.Chrome()
    
    driver.get("https://www.99acres.com/")
    
    time.sleep(5)
    
    print(driver.title)
    
    driver.execute_script("window.scrollBy(0, 1000);") 
    
    time.sleep(2)
    
    search_bar = driver.find_element(By.ID,"keyword")
    print(search_bar.get_attribute('placeholder'))
    
    # Click search bar to enter data
    search_bar.click()
    
    # Enter Society Name in search
    search_bar.send_keys(societyName)
    
    # Wait for suggestions
    time.sleep(10)
    
    # Click first available suggestion
    search_suggestions =driver.find_element(By.CSS_SELECTOR,"#suggestions_custom")
    first_search_suggestion = search_suggestions.find_element(By.TAG_NAME, 'li')
    first_search_suggestion.click()

    # Press enter search_bar
    search_bar.send_keys(Keys.RETURN)

    time.sleep(10)
    # Error occurs here
    # This page can not be loaded
    url = driver.current_url
    
    extract_data(driver.page_source)

我原以为会加载重定向的页面,但它没有加载,而是出现了此错误 “该页面无法加载。”

python-3.x selenium-webdriver selenium-chromedriver
1个回答
0
投票

我认为服务器正在阻止 Selenium 驱动程序机器人。因此,每当我尝试时,页面都无法加载。

我找到了加载页面的方法。重定向后,我关闭了驱动程序,实例化了一个新的 Chrome 驱动程序,并使用它来加载驱动程序。虽然这不是最好的方法,但这是有效的。

© www.soinside.com 2019 - 2024. All rights reserved.