Programming Xiaobai's Self-study Notes 12 (Python Crawler Introduction 4 Selenium Usage Example 2)

Series Article Directory

Programming Xiaobai's Self-study Notes Eleven (Python Crawler Introduction 3 Selenium Use + Example Detailed Explanation)

Programming Xiaobai's self-study notes ten (introduction to python crawler II + detailed explanation of example code) 

Programming Xiaobai's self-study notes 9 (introduction to python crawler + detailed code explanation) 


Table of contents

Series Article Directory

foreword

1. Use Selenium to open subpages

2. Use Selenium to realize webpage scrolling

3. Use selenium to operate the drop-down list

Summarize


foreword

Yesterday, I learned to use Selenium-related functions to open Baidu webpages and enter the text we want in the input box. Today we will learn more.


1. Use Selenium to open subpages

What is a subpage: There are multiple subpages in a page, which usually means that the main content of a website or webpage is divided into several sections, and the embodiment of each section in the webpage is a column. We have taken Baidu Tieba as an example, and its advertisement column is a subpage.

 

It can be seen that the subpage also has an ID, so we can lock the subpage through the ID,

code show as below:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver= webdriver.Chrome()
driver.get('https://tieba.baidu.com/')
element = driver.find_element(By.ID, 'iframeu6739266_0')
driver.switch_to.frame(element)
driver.find_element(By.ID, 'title0').click()
time.sleep(50)

The result is shown in the figure: 

 

We can see that we opened the first ad in the ad bar. Let's take a closer look at the code: 

The code "element = driver.find_element(By.ID, 'iframeu6739266_0')" locks the position of the sub-page.

"driver.switch_to.frame(element)" means to switch to a sub-webpage, and the parameter element must be a WebElement object, indicating the iframe element to be switched to .

"driver.find_element(By.ID, 'title0').click()" means click on the first ad.

P s: I am a novice, and I don’t know much about webpage codes. Referring to the case on the first day, can’t we directly lock the ID of the first ad and click it? Then I just use the code, unfortunately, can't get the desired result.  

2. Use Selenium to realize webpage scrolling

Not much to say, first on the code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver= webdriver.Chrome()
driver.get('https://blog.csdn.net/m0_49914128?type=blog')
for i in range(0,100,5):
    js= f'window.scrollTo(0,{i*100})'
    driver.execute_script(js)
    time.sleep(3)
 
 

After the program is running, it automatically looks down at the content of the web page, as if we were scrolling the mouse.

I will not take the screenshot when I run it, but my personal homepage is scrolling down.

Let's analyze the code carefully:

The front is easy to understand, mainly explain the content in the for loop

"driver.execute_script(js)", driver.execute_script() is a method provided by Selenium WebDriver to execute JavaScript code on the current page. Its syntax is as follows: driver.execute_script(script, *args) Among them, script is the JavaScript code to be executed, and *args is an optional parameter for passing to the JavaScript code.

Then it means that "f'window.scrollTo(0,{i*100})'" is actually a string of JavaScript codes, and its function is to scroll the page to the specified coordinates. Among them, 0 is the abscissa, and i*100 is the ordinate.

3. Use selenium to operate the drop-down list

When I saw this function, the first thing I thought was that I found the location, and then used click() to click and then select no. Then I did the actual operation, and I could indeed click the drop-down list, because I didn’t learn enough. Clicking on the item to be selected in the drop-down list will not work. So I tried N methods and finally succeeded. The code is like this:

driver.find_element(By.ID,'cardType').click()

driver.find_element(By.CSS_SELECTOR, "option[value='B']").click()

or replace with

driver.find_element(By.XPATH,"//option[@value='B']").click()

also.

 Let's take a look at the aspects specific to the drop-down table. The complete code is as follows:

 

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
driver= webdriver.Chrome()
driver.get('https://kyfw.12306.cn/otn/regist/init')
select = Select(driver.find_element(By.ID,'cardType'))
select.select_by_value("护照")
time.sleep(30)

I thought it would automatically select the passport as the document type, but an error was reported, "passport" could not be found, and the "select.select_by_value("passport")" code was replaced with "select.select_by_visible_text("passport")" to run successfully. As shown below 

 


Let's analyze the code:

" driver.find_element(By.ID,'cardType') " is the position of the selected drop-down list,  the Select () function creates a Select object, which can be used to operate the options in the drop-down menu, "select.select_by_visible_text("passport ")" realizes the function of selecting a passport.

 In order to find out why the value cannot be found, I added a line of code, "print(select.options)", select.options returns a list of all options, and I output it to see what the result is:

 [<selenium.webdriver.remote.webelement.WebElement (session="7792dee3dda6be18ea0d6894ce7884d9", element="4214676d-d07d-456a-b12b-cbf0cb593edf")>, <selenium.webdriver.remote.webelement.WebElement (session="7792dee3dda6be18ea0d6894ce7884d9", element="d768f3b0-ab39-473e-bf63-8969890d8886")>, <selenium.webdriver.remote.webelement.WebElement (session="7792dee3dda6be18ea0d6894ce7884d9", element="62c2afd7-edc3-42c0-8109-8e67db452d4c")>, <selenium.webdriver.remote.webelement.WebElement (session="7792dee3dda6be18ea0d6894ce7884d9", element="b95f5b92-b4dc-4173-b449-722b7f7689a2")>, <selenium.webdriver.remote.webelement.WebElement (session="7792dee3dda6be18ea0d6894ce7884d9", element="d148fddc-cff7-414c-9bec-45c9c9e22611")>, <selenium.webdriver.remote.webelement.WebElement (session="7792dee3dda6be18ea0d6894ce7884d9", element="40e4175a-2d8b-4bee-bada-8bbb71c886f3")>]

 "Passport" is indeed not found, and select.select_by_visible_text( " Passport " ) indicates that the option is selected through the visible text, "Passport" is visible to us, so it runs successfully.


Summarize

The above is what I want to talk about today. This article only briefly introduces the use of Selenium , and we will study in depth later.

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/131970039