簡體   English   中英

如何通過 selenium-webdriver 在 url http://www.spicejet.com/ 上執行多個操作並單擊帶有文本的鏈接作為會員登錄

[英]How to perform multiple actions and click on the link with text as Member Login on the url http://www.spicejet.com/ through selenium-webdriver

在此處輸入圖片說明

我嘗試了以下代碼,但它不是鼠標懸停並單擊“會員登錄”

WebElement lgn = driver.findElement(By.id("ctl00_HyperLinkLogin"));
WebElement ssm = driver.findElement(By.xpath("//a[contains(text(), 'SpiceCash/SpiceClub Members')]"));
WebElement cgm = driver.findElement(By.xpath("//a[contains(text(),'Member Login')]"));
Actions a1 = new Actions(driver);
a1.moveToElement(lgn).moveToElement(ssm).moveToElement(cgm).click().build().perform();

要以成員登錄名調用元素上的click() ,首先必須將鼠標懸停在文本為LOGIN / SIGNUP的元素上,然后將鼠標懸停在文本為SpiceCash/SpiceClub 成員的元素上,然后為該元素引入WebDriverWait文本作為會員登錄可點擊,您可以使用以下解決方案:

  • 代碼塊:

     import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Spicejet_member_login { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\\\Utility\\\\BrowserDrivers\\\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.spicejet.com/"); new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.link#ctl00_HyperLinkLogin")))).build().perform(); new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='hide-mobile']/a[contains(.,'SpiceCash/SpiceClub Members')]")))).build().perform(); new WebDriverWait(driver, 7).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='hide-mobile']//ul/li/a[@href='https://book.spicejet.com/Login.aspx' and contains(.,'Member Login')]"))).click(); } }
  • 瀏覽器快照:

spicejet_member_login

您可以嘗試在 moveToElement() 調用之間添加等待

WebDriverWait wait = new WebDriverWait(WebDriverRunner.getWebDriver(), 10); wait.until(ExpectedConditions.visibilityOf(element))

其中“元素”是應該出現在懸停時的菜單。

或者您可以使用現成的解決方案Selenide框架,該框架構建在 Selenium 之上,並內置了懸停方法和等待,有助於處理頁面動態通過此鏈接,您可以找到懸停()方法使用的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM