如何从网站获取数据并在 excel 页面对象模型中打印,数据驱动和页面工厂这是混合框架
Posted
技术标签:
【中文标题】如何从网站获取数据并在 excel 页面对象模型中打印,数据驱动和页面工厂这是混合框架【英文标题】:How to fetch data from website and print in excel Page object model, Data driven and page factory this is hybrid framework 【发布时间】:2022-01-14 22:43:21 【问题描述】:**Element Page**
public class BasePage
@FindBy(className = "_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy")//Signin Button
private WebElement SigninButton;
public void clickOnSigninButton()
SigninButton.click();
@FindBy(className= "pam _3-95 _9ay3 uiBoxRed")//Get Username erroe msg
private WebElement geterrormsg;
public String ClickonAdmissionnumber()
geterrormsg.getText();
**Another Class Test case page**
public class Tc_1
public void LoginTC1() throws Exception
**//create page objects**
BasePage lp = new BasePage();
**//invoke the methods**
lp.clickOnSigninButton();
lp.Errormsg();
**//write actual data to excel**
ExcelLibrary.writeData("Sheet1", 1, 5, Actualmsg);
试图从这个类属性中获取文本?我尝试过获取属性值但无法获取数据。请帮帮我.... 文本输出“错误的凭据无效的用户名和密码”。
【问题讨论】:
请详细说明“无法获取数据”的含义。使用 URL 或页面源代码和堆栈跟踪更新您的问题。 顺便说一句,不要使用首字母大写来命名对象。见javatpoint.com/java-naming-conventions 我建议您接受给定的答案。这可能会增加您获得好答案的机会... @pburgr 我已更新,请检查一次 在我的情况下,登录页面具有不同的外观,并且“错误凭据”文本位于密码输入和登录按钮之间。您使用什么浏览器进行测试? 【参考方案1】:代码:
package selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class SaikumarFbTest extends WebDriverSetup
public static void main(String[] args)
// wrapped driver initialization
WebDriver driver = startChromeDriver();
//entering url
driver.get("https://www.facebook.com/login");
// accept cookie consent in CZ
driver.findElement(By.xpath("//button[contains(@data-testid,'cookie-policy-dialog-accept-button')]")).click();
// switch to english using link in footer
driver.findElement(By.xpath("//a[contains(@href,'https://www.facebook.com/login')]")).click();
//accept cookie consent in EN
driver.findElement(By.xpath("//button[contains(@data-testid,'cookie-policy-dialog-accept-button')]")).click();
// click login button
driver.findElement(By.id("loginbutton")).click();
// get <div class="clearfix _5466 _44mg" id="email_container"></div>
WebElement emailContainer = driver.findElement(By.id("email_container"));
// get child divs
List<WebElement> emailContainerChildDivs = emailContainer.findElements(By.tagName("div"));
// print error text
System.out.println(emailContainerChildDivs.get(1).getText());
driver.quit();
输出:
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@#947) on port 23066
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 10, 2021 11:30:39 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
The email or mobile number you entered isn’t connected to an account. Find your account and log in.
要将文本保存到 XLSX 文件,请使用 Apache POI -> https://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi。
【讨论】:
它的页面对象模型、页面工厂和数据驱动框架我需要放入该框架中【参考方案2】:**Element Page**
public class BasePage
@FindBy(className = "_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy")//Signin Button
private WebElement SigninButton;
public void clickOnSigninButton()
SigninButton.click();
@FindBy(className= "pam _3-95 _9ay3 uiBoxRed")//Get Username erroe msg
private WebElement geterrormsg;
public String ClickonAdmissionnumber()
return geterrormsg.getText();
**Another Class Test case page**
public class Tc_1
public void LoginTC1() throws Exception
**//create page objects**
BasePage lp = new BasePage();
**//invoke the methods**
lp.clickOnSigninButton();
String Actualmsg = lp.Errormsg();
**//write actual data to excel**
ExcelLibrary.writeData("Sheet1", 1, 5, Actualmsg);
【讨论】:
以上是关于如何从网站获取数据并在 excel 页面对象模型中打印,数据驱动和页面工厂这是混合框架的主要内容,如果未能解决你的问题,请参考以下文章
如何从网站获取文本数据并使用python存储为excel文件