preface
Today, when I was working on the verification code identification project, I encountered a problem at the beginning. I reported all kinds of errors. I studied and inquired for a long time and finally solved it. I'll summarize and share it here.
Problems encountered and Solutions
When I locate the slider element of the verification code, the error of my positioning statement is always displayed. I have failed to debug the positioning method and path for many times. Later, I found that the part of the verification code here is written in iframe, that is, the sub Frame, which is equivalent to the sub page of the page. Its structure is completely consistent with the external web page. After opening the web page, selenium operates in the parent Frame by default, Therefore, the node of the child Frame cannot be obtained.
You need to use switch_ Switch to iframe in the mode of to. Frame():
from selenium import webdriver broswer = webdriver.Chrome() broswer.get('Web link') broswer.switch_to.frame('iframe of id perhaps name value')
However, the final error is displayed here: NoSuchFrameException: Message: tcaptcha_iframe_dy, after query and comparison, it is found that there are two iframes in this page
At this time, I thought it was OK to locate the one close. Unfortunately, the same error was reported. I can only find out the reason and finally know the problem. Here's a reference: selenium.common.exceptions.NoSuchFrameException: Message:xxx_liulanba blog - CSDN blog
I drew two with a hundred million pictures.
Iframes can be nested in two ways :
1. Parallel nesting
2. Hierarchical nesting
Hierarchical nesting needs to enter layer by layer, and finally exit layer by layer. As a result, iframe cannot be located after trying according to the method. Fortunately, the problem can be solved as long as you check the probability. After all, there are still many excellent bloggers on csdn. After analysis and comparison, it is found that the iframe tag attribute here is dynamic, and the positioning method should be changed: iframe in selenium reports an error NoSuchFrameException: Message: no such frame: element is not a frame_Binyee blog - CSDN blog Here I found a solution to locate the iframe tag directly:
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get("Web link") iframe = driver.find_elements(By.TAG_NAME, "iframe")[0] broswer.switch_to.frame(iframe)
But interestingly, I have a new error report when using: IndexError: list index out of range. There are two possibilities for this error report: 1. The index is out of range; 2. The list is empty and there are no elements. I guess this tag is not found at all. The reason is that it is actually parallel nesting, but I misunderstood it. I used the method of hierarchical nesting. I entered the first iframe first and didn't return to the Frame, so I couldn't find the second tag element.
The final code is as follows
from selenium import webdriver from selenium.webdriver.common.by import By import time broswer = webdriver.Chrome() broswer.get('https://maoyan.com/board/4?offset=100') time.sleep(2) iframe = broswer.find_elements(By.TAG_NAME, "iframe")[0] broswer.switch_to.frame(iframe) # You can also write that # iframe = broswer.find_elements(By.TAG_NAME, "iframe") # broswer.switch_to.frame(iframe[0]) element = broswer.find_element(By.XPATH, '//head/meta') # Get list element information print(element.get_attribute('charset')) # Return to the default frame broswer.switch_to.default_content() elements = broswer.find_element(By.XPATH, '//head/meta') print(elements.get_attribute('charset'))
The test is successful. The attribute content here is uppercase in the parent frame and lowercase in the child frame:
Here I would like to thank the above bloggers and welcome your correction and exchange.