why python selenium get the empty page_source?

241 views
Skip to first unread message

Zhe Lee

unread,
Oct 3, 2017, 10:20:36 PM10/3/17
to Selenium Users
I try to simulate the buy item operation on the link below. (Need login in first)


And after you click the button below. 

img_link:



But if I print out the page_source now I will get the empty value. 

Why this happen?

And of course any element location will lead to the :

**selenium.common.exceptions.NoSuchElementException**

I search on Google and I know that it's not caused by the unload of the page. 

So why this happen and how to  fix it? 


Here is the code I use. 
The account name and the code are in the cfg file.



    # -*- coding: utf-8 -*-
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import os
    import time
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.keys import Keys  #需要引入keys包
    chromedriver = "chromedriver.exe"
    os.environ["webdriver.chrome.driver"] = chromedriver
     
    d = {}
    with open("cfg.ini", encoding = 'utf-8') as f:
        for line in f:
           (key, val) = line.split()
           d[key] = val
    
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--ignore-ssl-errors')
    driver = webdriver.Chrome(chrome_options=options)
    
    driver.maximize_window()
    time.sleep(2)
    driver.find_element_by_id('J_Quick2Static').click()
    driver.find_element_by_id("TPL_username_1").clear()
    driver.find_element_by_id("TPL_username_1").send_keys(d['accountname'])
     
    driver.find_element_by_id("TPL_username_1").send_keys(Keys.TAB)
    time.sleep(1)
    driver.find_element_by_name("TPL_password").send_keys(d['code'])
    
    driver.find_element_by_css_selector('#J_SubmitStatic').click()
    
    print('-' * 20)
    print(d['phone'])
    driver.get(d['itemurl'])
    
    element=WebDriverWait(driver,60).until(lambda driver :
    driver.find_element_by_css_selector('#J_LinkBuy'))
    
    # with open('res1.html', 'a') as the_file:
       # the_file.write(driver.page_source)
    element.click()
    
    element=WebDriverWait(driver,60).until(lambda driver :
    driver.find_element_by_css_selector('#J_phone'))
    
    driver.find_element_by_id('J_phone').click()
    
    print('here is the page')
    print(driver.page_source)


The cfg file is like this :

    accountname xxx
    code xxxx
    phone xxxx
    itemurl xxxx

Zhe Lee

unread,
Oct 8, 2017, 1:12:48 AM10/8/17
to Selenium Users
And I try to change the frame of the web but it doesn't work. Why is that ? 
Reply all
Reply to author
Forward
0 new messages