1 回答
TA贡献1876条经验 获得超6个赞
这是没有任何问题的代码段。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
url = "https://stackoverflow.com/questions/61240545/get-json-data-from-driver-console-in-selenium-python"
driver.get(url)
elems = driver.execute_script("return document.getElementsByClassName('post-tag js-gps-track')")
for elem in elems:
print(elem.text)
driver.quit()
输出:
javascript
python
json
selenium
console.log
Process finished with exit code 0
编辑1:
如果您想获得特定的匹配元素,请使用以下内容。
value = driver.execute_script("return document.getElementsByClassName('name_of_class')[0].innerHTML")
# or you can also use the below if you are interested in first item only
value = driver.execute_script("return document.querySelector('.single_class')[0].innerHTML")
# query selector with multiple classes (class name with whitespaces)
value = driver.execute_script("return document.querySelector('.class1.class2')[0].innerHTML")
添加回答
举报