Reputation: 25
I'm trying to locate an element on a page. If I do Inspect Element, I can see the HTML code and the XPATH, but when I view page source, there is only Javascript code. Any idea on how to see the HTML page instead?
This is the page: https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50E42369DP1388723#/checkout/login
Here is the code. Basically I need to find the element and fill out the email and password field
while True:
try:
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="email"]')))
login_id = browser.find_element_by_xpath('//*[@id="email"]')
browser.execute_script("arguments[0].setAttribute('value', 'email address')", login_id)
login_password = browser.find_element_by_xpath('//*[@id="password"]')
browser.execute_script("arguments[0].setAttribute('value', 'enter password)", login_password)
account = browser.find_element_by_id('btnLogin')
browser.execute_script("arguments[0].click();", account)
break
except Exception as e:
print('Error: Couldnt login')
Upvotes: 1
Views: 9721
Reputation: 2938
Hi Christian you are doing every thing correct above but there is one catch on the given web page, the area where you are entering username and password lies inside a iframe =(iframe can be think of html page inside an html page).hence before performing any action you have to switch to that iframe first.
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\eclipseProject\\###\\src\\com\\smokealignstar\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50E42369DP1388723#/checkout/login");
// A short way to identify how many iframe's are present on a web page
List<WebElement> numberOfFrames= driver.findElements(By.tagName("iframe"));
System.out.println("Total Number of iframes present are : " +numberOfFrames.size());
for(int i=0;i<numberOfFrames.size();i++){
// here u can identify iframes with any of its attribute vale say name,title or which is most suitable.
System.out.println("Name of the i-frames : " + numberOfFrames.get(i).getAttribute("name"));
}
// Back to your question - n your case given area of the web page lies inside iframe hence
// key here is before entering username and password you have to switch to the frame first
driver.switchTo().frame(driver.findElement(By.name("injectedUl")));
driver.findElement(By.id("email")).sendKeys("username working");
driver.findElement(By.id("password")).sendKeys("password working");
}
for more info on various ways to work with iframe please go to below url
http://stackoverflow.com/questions/20069737/how-to-identify-and-switch-to-the-frame-in-selenium-webdriver-when-frame-does-no
Upvotes: 1
Reputation: 135
Better you wait till page loads completely and then switch to frame.and do operations.
IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
//check for complete load
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
//need to switch to iframe here WebElement frame = driver.findElement(By.xpath("//iframe[@title='PayPal - Log In']")); driver.switchTo().frame(frame);
webElment we1= driver.findElement(By.id("email"));
we1.clear();
we1.sendKeys("[email protected]");
webElment we2=driver.findElement(By.id("password"));
we2.clear();
we2.sendKeys("12345");
driver.findElement(By.id("btnLogin")).click();
Upvotes: 2
Reputation: 34
WebElement mast_mod = driver.findElement(By.xpath(".//*[@id='header_dropdown']"));
driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS);
Actions menele = new Actions(driver);
menele.moveToElement(mast_mod).click().build().perform();
driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='jq-dropdown-1']/ul/li[4]")).click();
driver.findElement(By.id("email")).sendKeys("demo");
driver.findElement(By.id("password")).sendKeys("12345");
driver.findElement(By.id("btnLogin")).click();
Hope this solution need and will help you. If this will help you please like.
Upvotes: 0
Reputation: 17553
I don't know why you are using execute_script. You just need to use sendkeys to set your values over the webpage.
Other one important thing is that there is an iframe present in the HTML DOM so you need to switch over it first.
Below is the java code which is working fine for me
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement frame = driver.findElement(By.xpath("//iframe[@title='PayPal - Log In']"));
driver.switchTo().frame(frame); //Switch to frame here
driver.findElement(By.id("email")).sendKeys("demo");
driver.findElement(By.id("password")).sendKeys("12345");
driver.findElement(By.id("btnLogin")).click();
Python code:-
driver.implicitly_wait(30)
frame = driver.find_elements_by_xpath("//iframe[@title='PayPal - Log In']")
driver.switch_to_frame(frame)
driver.find_element_by_id("email").send_keys('demo')
driver.find_element_by_id("password").send_keys('12345')
driver.findElement(By.id("btnLogin")).click();
Hope it will help you :)
Upvotes: 2