我想在執行自動化以增加頁面高度的同時使用 Python Selenium 更改 css 元素樣式。元素的html如下:
<div style="overflow-y:scroll;overflow-x:hidden;height:150px;">
我知道我可以使用類似下面的代碼來做到這一點:
driver.execute_script("document.getElementById('id name').style.height = '2000px';")
或者
driver.execute_script("document.getElementByClassName('class name').style.height = '2000px';")
但是在 HTML 中沒有id或class(只有style)。
uj5u.com熱心網友回復:
如果沒有ID或class存在,請使用CSS
driver.execute_script("document.getElementByCss('div[style='overflow-y:scroll']').style.height = '2000px';")
但是你應該檢查 HTML DOM 中提到的 CSS :
請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上CSS看看,如果你需要的element是越來越強調與1/1匹配的節點。
CSS 你應該檢查:
div[style='overflow-y:scroll']
或者
div[style='overflow-y:scroll'][sytle$='height:150px;']
或者
div[style='overflow-y:scroll;overflow-x:hidden;height:150px;']
無論哪個具有獨特的條目放入其中execute_script,您應該很高興去追求它。
替代方法
element = driver.find_element_by_css_selector("div[style='overflow-y:scroll;overflow-x:hidden;height:150px;']")
driver.execute_script("arguments[0].setAttribute('style','overflow-y:scroll;overflow-x:hidden;height:2000px;')", element)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338304.html
上一篇:收集瀏覽器中顯示但不回應的資料
