当元素在页面中实际可见时,selenium throw元素不可见异常错误

时间:2017-12-04 16:50:17

标签: java selenium selenium-webdriver paypal webdriver

我正在尝试使用给定的URL登录沙箱Paypal应用程序但是,当尝试使用以下代码在页面中查找电子邮件ID和密码字段时,它会抛出元素不可见的异常错误。

重现问题的代码

driver.get("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-7NA824468C3295748#/checkout/login");
Thread.sleep(20000);
driver.findElement(By.cssSelector("input[id='email']")).sendKeys("shariftest@gmail.com");
driver.findElement(By.cssSelector("input[id='password']")).sendKeys("test@1234");
driver.findElement(By.id("btnLogin")).click();
Thread.sleep(6000);

非常奇怪的是,这些元素定位器绝对正确且在页面中可见,但在尝试使用selenium查找元素时仍然会出错。

请尝试以上代码重现此问题。

2 个答案:

答案 0 :(得分:1)

我查看了源代码html,两个输入字段都以import numpy as np def find_matching_numbers(numbers): tempList = [[numbers[0]]] for value in numbers[1:]: for x in tempList: # tempMean : average of current list tempMean = np.mean(x) # if the number is not alone, we're searching from # the average of the list, in both directions (min and max) # with a range of half our initial range search # else if the number is alone, we're searching as if it is # considered as the min AND the max of the range. if len(x) > 1 and value >= tempMean - 1.5 and value <= tempMean + 1.5 or\ len(x) == 1 and value >= tempMean - 3 and value <= tempMean + 3: x.append(value) # Adding the value to the end of our rangesList, # since it could be a useful value in another context with # other datas. tempList.append([value]) # Keeping lists with highest length. maxLen = max([len(items) for items in tempList]) ret = [] for items in tempList: if len(items) == maxLen: ret.append(items) # If multiple lists with highest length, keep highest one. if type(ret[0]) == type([]) and len(ret) > 1: ret = max(ret) return ret 为样式。这就是为什么你无法向他们发送凯斯的原因。

display: none

您可以使用JavaScript设置值,例如电子邮件字段:

<input id="email" name="login_email" type="email" value="" autocomplete="off" placeholder="Email" style="display:none">

答案 1 :(得分:0)

尝试使用Javascript实现:

JavascriptExecutor jse = (JavascriptExecutor)driver
jse.executeScript("document.getElementById('email').setAttribute('value', 'shariftest@gmail.com')");