Java Selenium,如何从链接WebElement

时间:2015-04-22 12:04:00

标签: java selenium selenium-webdriver

我有一个包含url找到的链接的WebElement。我可以通过以下方式提取网址:

element.getAttribute("href");

但问题是:如何提取它的主播,我尝试这样:

webElement.getAttribute("linkText");

它给了我空值。我100%确定此链接有一个锚点。有没有办法获得锚?它更复杂,但示例简化代码可能如下所示:

WebDriver driver = new FirefoxDriver();
        driver.get("http://stackoverflow.com/questions/tagged/java");
        WebElement link  = driver.findElement(By.linkText("Bicycles"));

        System.out.println(link.getAttribute("href")); // shows http://bicycles.stackexchange.com/
        System.out.println(link.getAttribute("linkText")); // shows null

4 个答案:

答案 0 :(得分:1)

By&#34; Anchor&#34;我想你的意思是链接的文字?如果是,那么您可以使用public class ContactFetcher { private Context context; public ContactFetcher(Context c) { this.context = c; } public ArrayList<MainContact> fetchAll() { ArrayList<MainContact> listContacts = new ArrayList<MainContact>(); CursorLoader cursorLoader = new CursorLoader(context, RawContacts.CONTENT_URI, null, // the columns to retrieve (all) null, // the selection criteria (none) null, // the selection args (none) null // the sort order (default) ); Cursor c = cursorLoader.loadInBackground(); if (c.moveToFirst()) { do { MainContact contact = loadContactData(c); listContacts.add(contact); } while (c.moveToNext()); } c.close(); return listContacts; } private MainContact loadContactData(Cursor c) { // Get Contact ID int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID); String contactId = c.getString(idIndex); // Get Contact Name int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); String contactDisplayName = c.getString(nameIndex); MainContact contact = new MainContact(contactId, contactDisplayName, null, null, null, true); fetchContactNumbers(c, contact); fetchContactEmails(c, contact); return contact; } public void fetchContactNumbers(Cursor cursor, MainContact contact) { // Get numbers final String[] numberProjection = new String[]{Phone.NUMBER, Phone.TYPE,}; Cursor phone = new CursorLoader(context, Phone.CONTENT_URI, numberProjection, Phone.CONTACT_ID + " = ?", new String[]{String.valueOf(contact.id)}, null) .loadInBackground(); if (phone.moveToFirst()) { final int contactNumberColumnIndex = phone.getColumnIndex(Phone.NUMBER); final int contactTypeColumnIndex = phone.getColumnIndex(Phone.TYPE); while (!phone.isAfterLast()) { final String number = phone.getString(contactNumberColumnIndex); final int type = phone.getInt(contactTypeColumnIndex); String customLabel = "Custom"; CharSequence phoneType = Phone.getTypeLabel( context.getResources(), type, customLabel); contact.addNumber(number, phoneType.toString()); phone.moveToNext(); } } phone.close(); } public void fetchContactEmails(Cursor cursor, MainContact contact) { // Get email final String[] emailProjection = new String[]{Email.DATA, Email.TYPE}; Cursor email = new CursorLoader(context, Email.CONTENT_URI, emailProjection, Email.CONTACT_ID + "= ?", new String[]{String.valueOf(contact.id)}, null) .loadInBackground(); if (email.moveToFirst()) { final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA); final int contactTypeColumnIndex = email.getColumnIndex(Email.TYPE); while (!email.isAfterLast()) { final String address = email.getString(contactEmailColumnIndex); final int type = email.getInt(contactTypeColumnIndex); String customLabel = "Custom"; CharSequence emailType = Email.getTypeLabel( context.getResources(), type, customLabel); contact.addEmail(address, emailType.toString()); email.moveToNext(); } } email.close(); } } ,因为.getText()是块级元素。

<a>

答案 1 :(得分:1)

试试这个:

System.out.println(link.getText());

答案 2 :(得分:1)

您可以在此存储您的身份证明文字:

String text = driver.findElement(By.id("Text")).getText();
System.out.println(text);

答案 3 :(得分:0)

如果getText()返回空字符串,请尝试innerHTML属性:

String text = element.getAttribute("innerHTML")