叶夜还 发表于 2021-10-21 21:50:51

selenium函数打开浏览器后get操作无法打开指定网页

from selenium import webdriver
import random

def slp():
    sleep(random.randint(1,2))

driver_path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'#浏览器路径
driver = webdriver.Chrome(executable_path=driver_path)
driver.maximize_window()
slp()
driver.get('https://www.baidu.com/')
driver.refresh()
问问各位大佬为啥打开不了指定网页里?
运行后只在导航页待着

wp231957 发表于 2021-10-22 07:12:03

去掉12行

叶夜还 发表于 2021-10-25 21:08:27

wp231957 发表于 2021-10-22 07:12
去掉12行

去掉后还是不行

suchocolate 发表于 2021-10-29 08:22:21

本帖最后由 suchocolate 于 2021-10-29 08:24 编辑

去掉sleep或挪到后面,一般让selenium等待加载是get后sleep,或更稳妥的方法是用selenium的 WebDriverWait 和 expected_conditions。from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.baidu.com/')

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait


url = 'https://www.baidu.com'
browser = webdriver.Chrome()
browser.get(url)
wait = WebDriverWait(browser, 10)
wait.until(expected.visibility_of_element_located((By.ID, 'kw')))
ipt = browser.find_element_by_id('kw')
ipt.send_keys('Python')
ipt.send_keys(Keys.ENTER)
页: [1]
查看完整版本: selenium函数打开浏览器后get操作无法打开指定网页