why python selenium get the empty page_source?

2024/11/15 13:30:37

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: img

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
Answer

You have to switch to the new window to make it work:

driver.find_element_by_id('J_phone').click()driver.switch_to.window(driver.window_handles[1])print('here is the page')
print(driver.page_source)

In this way you tell Selenium with which window it must work.

https://en.xdnf.cn/q/119048.html

Related Q&A

How do i print the repetition output using regex it prints only first match

I have task where I need to print the occurrence and count and non-occurrence and count import resequence = 1222311m = re.search(r(\d)\1+,sequence)print(m) Exptected output : (1, 1) (3, 2) (1, 3) (2…

How do I get rid of all roles of the user discord.py

I was making my mute command and I thought of getting rid of all the members role but I dont know how to get rid of all the members roles I even tried for role in member.roles:await member.remove_roles…

How to pick the rows which contains all the keywords? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

Extract HTML Tables With Similar Data from Different Sources with Different Formatting - Python

I am trying to scrape HTML tables from two different HTML sources. Both are very similar, each table includes the same data but they may be structured differently, with different column names etc. For …

AttributeError: NoneType object has no attribute replace_with

I am getting the following error:Traceback (most recent call last):File "2.py", line 22, in <module>i.string.replace_with(i.string.replace(u\xa0, -)) AttributeError: NoneType object has…

How to expand out a Pyspark dataframe based on column?

How do I expand a dataframe based on column values? I intend to go from this dataframe:+---------+----------+----------+ |DEVICE_ID| MIN_DATE| MAX_DATE| +---------+----------+----------+ | 1|…

How can I trigger my python script to automatically run via a ping?

I wrote a script that recurses through a set of Cisco routers in a network, and gets traffic statistics. On the router itself, I have it ping to the loopback address of my host PC, after a traffic thre…

How do I make my bot delete a message when it contains a certain word?

Okay so Im trying to make a filter for my bot, but one that isnt too complicated. Ive got this:@bot.event async def on_message(ctx,message):if fuck in Message.content.lower:Message.delete()But it gives…

pyinstaller cant find package Tix

I am trying to create an executable with pyinstaller for a python script with tix from tkinter. The following script also demonstrates the error: from tkinter import * from tkinter import tixroot = ti…

form.validate_on_submit() doesnt work(nothing happen when I submit a form)

Im creating a posting blog function for social media website and Im stuck on a problem: when I click on the "Post" button(on create_post.html), nothing happens.In my blog_posts/views.py, when…