无法使用requests请求
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--user-data-dir=~/chromeTemp"); // applicable to windows os only
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");
这种方法在我调用本地浏览器的情况下是无法使用的,我这边是selenium执行js代码 使用XMLHttpRequest 发送请求,但是遇到跨域有啥解决方案么,
目前使用window.open是可以获取到数据的,但是不能一直频繁打开新页面再关闭
添加那个选项后可以跨域的,题主怎么用的,示例代码如下需要注意获取返回值要return TB(url)
from selenium import webdriver
import time
opt = webdriver.ChromeOptions()
opt.add_argument("--disable-web-security")
driver = webdriver.Chrome(options=opt)
driver.get('http://www.w3dev.cn/')
time.sleep(3)
url='https://www.baidu.com/'
js = '''
url = '%s'
function TB(url) {
var httpRequest = new XMLHttpRequest()
httpRequest.open('GET', url, false)
httpRequest.send()
var data = httpRequest.responseText
return data
}
return TB(url)
'''%url
html=driver.execute_script(js)
print(html)
跨域请求应该用python中的requests
不要用js代码发跨域请求