遇到错误:”定位器必须能够通过 `toJson` 方法序列化为 JSON。按标签名:Text”

huangapple 未分类评论46阅读模式
英文:

Getting error "Locator must be serializable to JSON using a `toJson` method. By.tagName: Text"

问题

我正在使用 Selenium 4.0.0-alpha-6 版本来使用相对定位器relative locators)。

当我尝试读取位于 `By.name("User")` 右侧的 `<Text>` 标签时我遇到以下错误

步骤复现

```java
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;

public void relativelocator() {
    WebElement element = returnappiumdriver().findElement(withTagName("Text").toRightOf(By.name("User")));

    System.out.println(" element " + element.getAttribute("Name"));
    System.out.println(" element " + element.getSize());
    System.out.println(" element " + element.getTagName());
    System.out.println(" element " + element.getLocation());

    String text = element.getText();

    System.out.println(" text " + element.getText());
}

错误信息:

java.lang.IllegalArgumentException: 定位器必须可通过 `toJson` 方法序列化为 JSON。By.tagName: Text
        
    at org.openqa.selenium.support.locators.RelativeLocator.assertLocatorCanBeSerialized(RelativeLocator.java:262)
    at org.openqa.selenium.support.locators.RelativeLocator.access$100(RelativeLocator.java:42)
    at org.openqa.selenium.support.locators.RelativeLocator$RelativeBy.<init>(RelativeLocator.java:78)
    at org.openqa.selenium.support.locators.RelativeLocator$RelativeBy.<init>(RelativeLocator.java:71)
    at org.openqa.selenium.support.locators.RelativeLocator$RelativeBy.<init>(RelativeLocator.java:66)
    at org.openqa.selenium.support.locators.RelativeLocator.withTagName(RelativeLocator.java:63)
    at com.manh.cp.mobileUI.util.Commonfunctions.testrelativelocator(Commonfunctions.java:989)
    at com.manh.cp.mobileUI.steps.PosStoreUIStepDefinitions.lambda$new$41(PosStoreUIStepDefinitions.java:270)
    at ✽.And Read next element of(Examples/000-Example-scenarios.feature:36)

<details>
<summary>英文:</summary>

I am using&#160;relative locators&#160;using&#160;selenium 4.0.0-alpha-6&#160; version. 

When I try to read a `&lt;Text&gt;` tag right of&#160;&#160;`By.name(&quot;User&quot;)`.&#160; I am getting the below error.

    
&gt; Locator must be serializable to JSON using a `toJson` method. By.tagName: Text&quot;.
        

Steps to reproduce:


    import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;

    public void relativelocator() {
    
        WebElement element= 
    **returnappiumdriver().findElement(withTagName(&quot;Text&quot;).toRightOf(By.name(&quot;User&quot;)));**
    
        System.out.println(&quot; element  &quot;+ element.getAttribute(&quot;Name&quot;));
        System.out.println(&quot; element  &quot;+ element.getSize());
    
        System.out.println(&quot; element  &quot;+ element.getTagName());
    
        System.out.println(&quot; element  &quot;+ element.getLocation());
    
        String text = element.getText();
    
        System.out.println(&quot; text  &quot;+ element.getText());
    }


Error I am getting:

    java.lang.IllegalArgumentException: Locator must be serializable to JSON using a `toJson` method. By.tagName: Text
        
    &#160; &#160; at org.openqa.selenium.support.locators.RelativeLocator.assertLocatorCanBeSerialized(RelativeLocator.java:262)
    	at org.openqa.selenium.support.locators.RelativeLocator.access$100(RelativeLocator.java:42)
    	at org.openqa.selenium.support.locators.RelativeLocator$RelativeBy.&lt;init&gt;(RelativeLocator.java:78)
    	at org.openqa.selenium.support.locators.RelativeLocator$RelativeBy.&lt;init&gt;(RelativeLocator.java:71)
    	at org.openqa.selenium.support.locators.RelativeLocator$RelativeBy.&lt;init&gt;(RelativeLocator.java:66)
    	at org.openqa.selenium.support.locators.RelativeLocator.withTagName(RelativeLocator.java:63)
    	at com.manh.cp.mobileUI.util.Commonfunctions.testrelativelocator(Commonfunctions.java:989)
    	at com.manh.cp.mobileUI.steps.PosStoreUIStepDefinitions.lambda$new$41(PosStoreUIStepDefinitions.java:270)
    	at ✽.And Read next element of(Examples/000-Example-scenarios.feature:36



</details>


# 答案1
**得分**: 0

Selenium-java-4.0.0-alpha-6.jar 文件,我们会得到以下错误:

java.lang.IllegalArgumentException: 参数类型非法:org.openqa.selenium.By$ByTagName

您需要从以下链接下载 Selenium-java-4.0.0-alpha-4.jar 文件,以解决问题:

https://selenium-release.storage.googleapis.com/index.html?path=4.0-alpha4/

<details>
<summary>英文:</summary>

Selenium-java-4.0.0-alpha-6.jar file, we will get below error:

java.lang.IllegalArgumentException: Argument is of an illegal type: org.openqa.selenium.By$ByTagName



You have to download Selenium-java-4.0.0-alpha-4.jar files from the below and clears your issue

https://selenium-release.storage.googleapis.com/index.html?path=4.0-alpha4/

</details>



huangapple
  • 本文由 发表于 2020年7月23日 13:08:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/63047247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定