gpt4 book ai didi

javascript - Java - 在 browser.execute(script) 中使用方法

转载 作者:行者123 更新时间:2023-12-02 11:30:37 25 4
gpt4 key购买 nike

我有一个带有包含名称的数组的方法,我正在使用 SWT 浏览器并使用 browser.execute() 我想填写表单。

我的问题:我想在 browser.execute() 部分中使用 generateName() 方法而不是 TEST 作为值。

方法

/**
* generate name, lastname
*/
public void generateName() {
Random generateName = new Random();
String[] name = {"Liam", "Elias", "Kevin", "Julian", "Linus", "Levi", "Jonas", "Daniel", "Milan", "Maria", "Alexander", "Manfred", "Max", "Jens", "Dennis", "Oliver", "Jan", "Bob", "Henry"};
String[] lastname = {"Meyer", "Müller", "Eilers", "Gerdes", "Schröder", "Janssen", "Ahlers", "Bruns", "Behrens", "Harms", "Schmidt", "Macke", "Kruse", "Becker", "Lange", "Suhr", "Schulte", "Weber", "Lampe"};

// debug
System.out.println(name[generateName.nextInt(19)] + " " + lastname[generateName.nextInt(19)]);
}

带有监听器的按钮

/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(800, 600);
shell.setText("SWT Application");

Browser browser = new Browser(shell, SWT.NONE);
browser.setUrl("www.pseudopage.com/SignUp");
browser.setBounds(10, 10, 780, 352);

Button btnNewButton = new Button(shell, SWT.BORDER);
btnNewButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
browser.execute("document.getElementById('txtFirstName').value=\"TEST\"");
}
});
btnNewButton.setBounds(10, 374, 94, 28);
btnNewButton.setText("Generate");

}

最佳答案

我不确定我是否理解你的问题。所以希望我能理解:

你可以:

browser.execute("document.getElementById('txtFirstName').value=\"" + generateName() + "\"");

但是generateName必须返回一个值而不是打印它..

public String generateName() {
Random generateName = new Random();
String[] name = ...
String[] lastname = ...

return name[generateName.nextInt(19)] + " " + lastname[generateName.nextInt(19)];
}

关于javascript - Java - 在 browser.execute(script) 中使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49323407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com