我試圖從 IntelliJ 運(yùn)行下面的代碼,但又出現(xiàn)錯誤(如下)。我只想使用 Xpath 定位器單擊網(wǎng)站上的按鈕并添加斷言來驗(yàn)證我的測試。構(gòu)建如此簡單的測試的最佳方法是什么?import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.testng.annotations.Test;import java.util.List;public class ButtonTest extends CommonScenario { private static WebDriver driver; @Test() public void button_test() { button b = driver.findElements(By.xpath("//button[text()='Teleworking")); }錯誤:Error:(16, 5) java: cannot find symbol symbol: class button location: class selenium.ButtonTest
3 回答

互換的青春
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個贊
沒有button
課。driver.findElements
返回一個列表WebElement
List<WebElement> elements = driver.findElements();
如果您想要單個元素,請使用driver.findElement
WebElement element = driver.findElement();

慕尼黑的夜晚無繁華
TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個贊
沒有名為 Button 的類來啟動對象 b。嘗試 WebElements。所以該方法的代碼是
public void button_test() { List<WebElement> b = driver.findElements(By.xpath("//button[text()='Teleworking")); }
請注意,您使用的是 findElements 而不是 findElement,后者將返回 webelement 列表而不是單個 webelement。

PIPIONE
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個贊
請檢查 xpath 是否有單個元素"//button[text()='Teleworking"
。如果是這樣,請像下面一樣更新您的腳本
WebElement button = driver.findElement(By.xpath("//button[text()='Teleworking"));
如果您有多個具有與上面相同的 xpath 的按鈕,那么您需要通過 List 來處理它,在這種情況下您可以使用下面的 XPath。
List<WebElement> button = driver.findElements(By.xpath("//button[text()='Teleworking"));
添加回答
舉報(bào)
0/150
提交
取消