【问题标题】:Assignment to constant not permitted error while writing value using Selenium and VBA使用 Selenium 和 VBA 写入值时分配给常量不允许的错误
【发布时间】:2021-05-04 23:27:03
【问题描述】:

我可以阅读但无法设置。为什么?

.FindElementByCss(".multiselect__single.per-page-text").text

返回 25

.FindElementByCss(".multiselect__single.per-page-text").text = 50

给予:

Assignment to constant not permitted

如何解决?

我在以下语句中收到 element not interactive 错误:

.FindElementByCss(".multiselect__single.per-page-text").SendKeys (50)

【问题讨论】:

  • WebDriver 命令发送 GET 请求以检索文本属性值 - 这就是您无法设置其值的原因。如果没有页面源/应用程序链接,就无法说明如何为您提供帮助。
  • 如果不是输入标签,则无法发送密钥。

标签: vba selenium selenium-webdriver xpath css-selectors


【解决方案1】:

您应该了解硒的工作原理

这是 selenium 通信的架构。

  1. 每个浏览器都有其可以与浏览器通信的驱动程序 并让它做一些动作。
  2. 驱动程序公开了它支持的方法,例如单击按钮、通过 API 发送额外的值
  3. 不同语言的Selenium库调用这个API,API调用对应的方法
  4. 调用该方法后,chromedriver 会与浏览器对话以执行此特定操作。

所以 .FindElementByCss(".multiselect__single.per-page-text").text

调用api查找元素并给出innerText,驱动程序调用特定方法联系浏览器获取信息,一旦将信息返回给客户端。

在 chromedriver API 中,.text 是一个常量或最终标识符,不能从客户端更改,因此不能从客户端修改。

但是没有什么能阻止你通过直接在浏览器上执行 javascript 来修改它。

driver.executeScript("arguments[0].text=arguments[1]",driver.FindElementByCss(".multiselect__single.per-page-text"),50);

【讨论】:

    【解决方案2】:

    此错误消息...

    Assignment to constant not permitted
    

    ...implies that 您试图为使用 Const 声明的变量或类型库常量分配新值。如果需要分配新值,请声明所需类型的普通变量并将值分配给该变量。


    您可以检索文本 25,它可能是一个 readonly 字段,您甚至无法手动设置任何其他值。但是,相关的 HTML 会帮助我们以更好的方式得出结论。

    结论/解决方案

    如果它是只读字段,您将无法设置您选择的任何其他文本。否则,如果字段中的值是可编辑的,要设置一个避免 element not interactive 的值,您需要使用SendKeys,您可以使用以下任一Locator Strategies

    • 使用FindElementByCss

      newHour = Hour(Now()) 
      newMinute = Minute(Now()) 
      newSecond = Second(Now()) + 10 
      waitTime = TimeSerial(newHour, newMinute, newSecond) 
      driver.Wait waitTime
      driver.FindElementByCss(".multiselect__single.per-page-text").SendKeys "50"
      
    • 使用FindElementByXPath

      newHour = Hour(Now()) 
      newMinute = Minute(Now()) 
      newSecond = Second(Now()) + 10 
      waitTime = TimeSerial(newHour, newMinute, newSecond) 
      driver.Wait waitTime
      driver.FindElementByXPath("//*[@class='multiselect__single per-page-text'][contains(@name, 'ContentPlaceHolder1')]").SendKeys "50"
      

    参考

    您可以在以下位置找到一些相关讨论:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多