英文:
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 relative locators using selenium 4.0.0-alpha-6  version.
When I try to read a `<Text>` tag right of  `By.name("User")`.  I am getting the below error.
> Locator must be serializable to JSON using a `toJson` method. By.tagName: Text".
Steps to reproduce:
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());
}
Error I am getting:
java.lang.IllegalArgumentException: Locator must be serializable to JSON using a `toJson` method. 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>
# 答案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>
专注分享java语言的经验与见解,让所有开发者获益!
评论