常見的Google 服務會拖慢 selenium 效能, 除了使用 adblock plus 來 block connection 也可以設定在 selenium 裡.
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["www.baidu.com"]})
driver.execute_cdp_cmd('Network.enable', {})
範例 2:
https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/387
import undetected_chromedriver as uc
driver = uc.Chrome()
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ['*png','*woff2','*woff','*jpg','https://www.apple.com/ac/globalnav/7/en_US/styles/ac-globalnav.built.css']})
driver.execute_cdp_cmd('Network.enable', {})
driver.get('https://apple.com/')
Java 版語法:
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v94.network.Network;
public class BlockURL {
@Test
public void blockUrl() {
System.setProperty("webdriver.chrome.driver", "path to chromedriver");
ChromeDriver driver = new ChromeDriver();
DevTools devTool = driver.getDevTools();
devTool.createSession();
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
// Blocks all css files
devTool.send(Network.setBlockedURLs(List.of("*.css")));
devTool.addListener(Network.loadingFailed(), loadingFailed -> {
System.out.println("Blocking reason: " + loadingFailed.getBlockedReason().get());
});
driver.get("https://url.com");
}
}
你好 max, 平常看你的部落格受益良多, 感謝。
想請問你最近使用uc的情況, 因為之前用uc都可以bypass cloudflare, 最近使用都無法, 或是目前有找到seleniumbase這個, 不過目前還沒有使用過, 是否能分享看法? 謝謝
from DrissionPage import ChromiumPage
https://github.com/g1879/DrissionPage/
import nodriver as uc
https://github.com/ultrafunkamsterdam/nodriver
上面這2個, 都可以 bypass.