【问题标题】:Setting Style of Css in python在python中设置CSS样式
【发布时间】:2021-05-25 10:57:13
【问题描述】:

这是我的代码(没有导入和其他东西):

driver.get('http://democaptcha.com/demo-form-eng/hcaptcha.html')
time.sleep(3)
driver.execute_script("document.getElementsByName('h-captcha-response').style.display='none';")

但这会给我带来错误:

selenium.common.exceptions.JavascriptException:消息:javascript 错误:无法设置未定义的属性“显示”

当我这样做时:

driver.execute_script("document.getElementById('h-captcha-response-0zu3aawejggj').style.display = 'block';")

它告诉我错误

selenium.common.exceptions.JavascriptException:消息:javascript 错误:无法读取 null 的属性“样式”

(参考代码)

Selenium (with python) how to modify an element css style

How can I change/remove style attibute with selenium python?

谢谢!

【问题讨论】:

  • getElementsByName 返回一个元素的array,而不是一个元素。你不能做getElementsByName().style。数组没有 .style 属性。

标签: javascript python selenium


【解决方案1】:

这是因为它找不到元素。可能是因为ID 不断改变每个页面加载。另一种选择可能是

driver.execute_script("document.querySelector(`[id^='h-captcha-response']`).style.display = 'block'");

这意味着它将找到第一个以h-captcha-response开头的ID的元素。

【讨论】:

    【解决方案2】:

    试试这个:

    driver.execute_script("document.getElementsByName('h-captcha-response')[0].style.display = 'block';")
    

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 2016-04-18
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2011-11-11
      • 2016-02-21
      • 2011-02-05
      • 2018-07-06
      相关资源
      最近更新 更多