无法使用selenium webdriver在Compose E-mail页面上的To(Email id)字段中发送密钥

To ”Buttom 无法从selenium webdriver 获取密钥 。 在输出中,它显示无法定位元素。 “To”在iframe中,我使用了I帧但是它也没用。

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class mail { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", "D:\mozilla driver\geckodriver.exe"); WebDriver driver=new FirefoxDriver(); driver.get("https://www.mail.com/int/"); driver.findElement(By.xpath(".//*[@id='login-button']")).click(); driver.findElement(By.xpath(".//*[@id='login-email']")).sendKeys("rahulrahulxyz@mail.com"); driver.findElement(By.xpath(".//*[@id='login-password']")).sendKeys("incredible"); driver.findElement(By.xpath(".//*[@id='login-form']/button")).click(); driver.switchTo().frame("thirdPartyFrame_home"); driver.findElement(By.linkText("Compose E-mail")).click(); Thread.sleep(5000); driver.switchTo().frame("thirdPartyFrame_mail"); // **here is error** driver.findElement(By.xpath(".//*[@id='idbd']/div[2]/div[1]/div[1]/div[2]/div/div/ul/li/input")).sendKeys("abcde@mail.com"); } } 

要通过sendKeys()方法发送Emailid ,您必须先切换回defaultContent,然后再使用WebDriverWait切换到预期的 ,最后引导WebDriverWait使To字段可以进行交互,然后按如下方式发送Emailid

  • 代码块:

     driver.switchTo().defaultContent(); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("thirdPartyFrame_mail"))); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='compose-header_item compose-header_to mailobjectpanel-objectivation mailobjectpanel-textfield js-component mailobjectpanel']//div[@class='select2-container select2-container-multi js-select2']/ul/li/input"))).sendKeys("abcde@mail.com"); 
  • 快照:

电子邮件ID