我尝试了所有可能的元素定位器,但在成功登录代码后,无法点击我想要它点击的元素。我收到以下错误:
...
class charSequence {
String str1="abcdef@gmail.com";
String str2= "abcdfghhh";
CharSequence cs1 = "int1";
CharSequence cs2 = "int2";
//boolean retval;
public charSequence(String str){}
}
public class QueuedRefund {
public static void main(String[] args) {
String[] temp1 = new String[1];
String[] temp2 = new String[1];
charSequence s1 = new charSequence("str1");
charSequence s2 = new charSequence("str2");
temp1[0]=s1.str1;
temp2[0]=s2.str2;
System.setProperty("webdriver.gecko.driver","/home/srvmedia/Geckodriver /geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://dadfd.mywebsite.in/merchant/login");
driver.findElement(By.name("email")).sendKeys(temp1);
driver.findElement(By.name("password")).sendKeys(temp2);
driver.findElement(By.id("loginForm")).submit();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("html/body/div[6]/div/div/div[1]/div[1]/div[2]/div/a[1]/div")).click();
//driver.findElement(By.xpath("//div[2]/div/a/div")).click();
}
}
答案 0 :(得分:0)
(1)您的问题是:成功登录后,代码无法单击我想要单击的元素。和 (2)您使用过“nosuchelementexception”标签。 根据这两个事实,我知道你在这一行上遇到错误: driver.findElement(By.xpath( “HTML /体/格[6] / DIV / DIV / DIV [1] / DIV [1] / DIV [2] / DIV / A [1] / DIV”))。点击();
如果您发布错误消息,了解问题并确认我的上述假设会很有帮助。
您的问题的答案取决于您使用的元素定位器(xpath)是否正确。我猜测xpath是不正确的,因为你可能得到了nosuchelementexception。
您可以在Firefox浏览器中使用FirePath加载项确认xpath是否正确。安装完成后,在名为“Firepath”的最后一个选项卡中输入xpath,然后按“回车”键。应突出显示相应的元素(您要在其上单击)。如果没有,那么xpath就不应该是它。
您还可以检查要单击的元素是否位于该页面上的不同框架中。在这种情况下,您需要先切换到该帧。该页面上有不同的框架吗?您也可以使用Firepath查看此内容。
如果您确定(并且已经使用Firepath验证过)xpath是正确的,那么请检查该元素是否已启用并根据该页面的来源显示。
Webelement element = driver.findElement(By.xpath("xpath_of_that_element"));
System.out.println("The element is enabled: " + element.isEnabled());
System.out.println("The element is displayed: " + element.isDisplayed());
我猜测上述两个打印语句中至少有一个会显示'false'。
如果xpath正确并且上面两个打印语句显示“true”,则尝试使用WebElement的getText()方法在该WebElement上打印文本(要在其上单击)。它成功了吗?我认为它不会,因为你可能会得到nosuchelementexception。
请注意:如果您获得的异常不是nosuchelementexception,则可以从问题中删除该标记。