I try to simulate the buy item operation on the link below. (Need login in first)
taobao_item_link
And after you click the button below.
img_link:
The link will jump to a new 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"] = chromedriverd = {}
with open("cfg.ini", encoding = 'utf-8') as f:for line in f:(key, val) = line.split()d[key] = valoptions = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(chrome_options=options)driver.maximize_window()
driver.get('https://login.taobao.com/member/login.jhtml')
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