Reprinted from: https://www.cnblogs.com/wuhl-89/p/7778463.html
View the element and find that the id is dynamic, so do not choose to locate by id.
Using the xpath path to locate, every time I get the element, it fails. Finally, I checked the Internet and found that it is nested in the form frame/iframe, so we need to enter the frame/iframe first, and then locate it
There are three positioning methods for frame/iframe, one is through id; the other is through name; the third is xpath
Example: switch_to_frame(id); or switch_to_frame(name); if the frame has no id and name, it can only locate the frame by xpath, switch_to_frame(xpath)
code show as below:
1 from selenium import webdriver 2 import time 3 4 url = 'http://www.126.com' 5 driver = webdriver.Firefox() 6 driver.get(url) 7 driver.switch_to.frame("x-URS-iframe") #Enter the form 8 time.sleep(5) 9 #Use Xpath to locate 10 driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/input').send_keys('11111')
The above solves the problem with Firefox, but the frame is not found on Chrome
Chrome solution:
In driver.switch_to.frame("x-URS-iframe"), the serial number of the frame can be written in the brackets. If there are multiple frames, the outermost one is 0, the inner one is 1, and so on.
So driver.switch_to.frame(0), and that's it.