Python+selenium+webdriver can't locate the elements of the page, and the newcomer has been bothered for several days by asking for help.

< H1 > using Python+selenium+webdriver for automated testing, it was found that the page elements could not be crawled. My process is like this: open the Baidu home page, enter segementfault to click on the search, select the first search result, enter the Sifu home page, no matter what I do, all the elements on the page can not be crawled, prompt me selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:. < / H1 > < H1 > but the strange thing is that if I go directly to the home page of Si No, without searching through Baidu, I can grab the elements of the page. The code is the same and no change has been made. < / H1 > < H1 > the code is as follows: < / H1 >

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox ()
driver.get ("https://www.baidu.com/")
print(driver.title)
driver.find_element_by_xpath("//*[@id="kw"]").send_keys("codeshelper")
driver.find_element_by_xpath("//*[@" Id= "su"]"). Click ()
time.sleep (3)
driver.find_element_by_xpath ("/ html/body/div [1] / div [5] / div [1] / div [3] / div [1] / h3) a"). Click ()
time.sleep (5)
driver.find_element_by_xpath ("/ html/body/div [4] / div/div/div [1] / div [2] / a [1]"). Click ()
driver.) Find_element_by_xpath ("/ html/body/div [4] / div/div/div [2] / div [4] / div [1] / div/a [2] / div [1] / h4"). Click ()

Mar.28,2021

it is recommended to print raw_html take a look at


need to switch windows_handle. Otherwise, the focus of chromedriver is still on Baidu that tab, can not locate the new tab page elements.
Baidu or Google keyword "selenium driver.current_window_handle"
try


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
print(driver.title)
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('segmentfault')
driver.find_element_by_xpath('//*[@id="su"]').click()
time.sleep(3)
driver.find_element_by_xpath("/html/body/div[1]/div[5]/div[1]/div[3]/div[1]/h3/a").click()
time.sleep(5)

driver.switch_to_window(driver.window_handles[-1])

driver.find_element_by_xpath('/html/body/div[4]/div/div/div[1]/div[2]/a[1]').click()
driver.find_element_by_xpath('/html/body/div[4]/div/div/div[2]/div[4]/div[1]/div/a[2]/div[1]/h4').click()

No problem has been tested-- replace Chrome with Firefox

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a540-2c1ed.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a540-2c1ed.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?