Selenium找不到元素,因为页面源代码在Javascript中



我正在尝试查找页面上的元素。如果我检查元素,我可以看到HTML代码和XPATH,但当我查看页面源代码时,只有Javascript代码。你知道如何查看HTML页面吗?

这是一个页面:https://www.paypal.com/cgi-bin/webscr?cmd=_express-结账&token=EC-50E42369DP1388723#/签出/登录

http://pastebin.com/BGWQikaZ

这是代码。基本上我需要找到元素并填写电子邮件和密码字段

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')

最好等到页面完全加载后再切换到frame.并执行操作。

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"));

//需要在此处切换到iframeWebElement 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("user@demo.com");
webElment we2=driver.findElement(By.id("password"));
we2.clear();
we2.sendKeys("12345");

driver.findElement(By.id("btnLogin")).click();

我不知道您为什么要使用execute_script。你只需要使用sendkey在网页上设置你的值。

另一件重要的事情是HTMLDOM中有一个iframe,所以您需要先切换它。

下面是java代码,它对我来说很好

    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代码:-

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();

希望它能帮助你:)

嗨,Christian,你上面做的每一件事都是正确的,但在给定的网页上有一个陷阱,你输入用户名和密码的区域位于iframe=中(iframe可以是html页面中的html页面)。因此,在执行任何操作之前,你必须先切换到该iframe。

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");
    }

有关使用iframe的各种方法的更多信息,请访问下面的url

http://stackoverflow.com/questions/20069737/how-to-identify-and-switch-to-the-frame-in-selenium-webdriver-when-frame-does-no
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();

希望这个解决方案需要并将帮助你。如果这对你有帮助,请喜欢。

最新更新