Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
JxBrowser DOM API allows working with HTML SELECT and OPTION elements. To work with SELECT element, the DOMSelectElement class is used. Let's see how to access SELECT element on web page that contains the following HTML code: (JxBrowser DOM API允许使用HTML SELECT和OPTION元素。要使用SELECT元素,请使用DOMSelectElement类。让我们看看如何访问包含以下HTML代码的网页上的SELECT元素:)
<select id='select-tag'> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </select>
To access SELECT element use the following JxBrowser API: (要访问SELECT元素,请使用以下JxBrowser API:)
DOMDocument document = browser.getDocument(); DOMSelectElement select = (DOMSelectElement) document.findElement(By.id("select-tag"));
Now, using the select instance you can get information about its options, programmatically select specified option. For example: (现在,使用select实例,您可以获得有关其选项的信息,以编程方式选择指定的选项。例如:)
List<DOMOptionElement> options = select.getOptions(); options.get(2).setSelected(true);
The complete sample you can find below: (您可以在下面找到完整的样本:)
import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.dom.By; import com.teamdev.jxbrowser.chromium.dom.DOMDocument; import com.teamdev.jxbrowser.chromium.dom.DOMOptionElement; import com.teamdev.jxbrowser.chromium.dom.DOMSelectElement; import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; import com.teamdev.jxbrowser.chromium.events.LoadAdapter; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; import java.util.List; /** * The sample demonstrates how to programatically select an option item in SELECT tag. */ public class DOMSelectOptionSample { public static void main(String[] args) { Browser browser = new Browser(); BrowserView browserView = new BrowserView(browser); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(browserView, BorderLayout.CENTER); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.addLoadListener(new LoadAdapter() { @Override public void onFinishLoadingFrame(FinishLoadingEvent event) { if (event.isMainFrame()) { Browser browser = event.getBrowser(); DOMDocument document = browser.getDocument(); DOMSelectElement select = (DOMSelectElement) document.findElement(By.id("select-tag")); selectOptionByIndex(select, 2); } } }); browser.loadHTML("<html><body><select id='select-tag'>\n" + " <option value=\"volvo\">Volvo</option>\n" + " <option value=\"saab\">Saab</option>\n" + " <option value=\"opel\">Opel</option>\n" + " <option value=\"audi\">Audi</option>\n" + "</select></body></html>"); } private static void selectOptionByIndex(DOMSelectElement select, int index) { List<DOMOptionElement> options = select.getOptions(); options.get(2).setSelected(true); } }