Java 为什么selenium getAttribute(“href”)不起作用?

Java 为什么selenium getAttribute(“href”)不起作用?,java,selenium,Java,Selenium,摘要:我的代码转到craigslist广告url。它提取广告正文中隐藏的电话号码。该代码适用于许多URL,但我在代码中包含的URL除外。(顺便说一句,您可以复制并运行我的代码,而无需编写任何其他代码。) 问题:仅此url的getAttribute(“href”)返回null。为什么?我该如何解决这个问题 代码: import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.seleni

摘要:我的代码转到craigslist广告url。它提取广告正文中隐藏的电话号码。该代码适用于许多URL,但我在代码中包含的URL除外。(顺便说一句,您可以复制并运行我的代码,而无需编写任何其他代码。)

问题:仅此url的
getAttribute(“href”)
返回
null
。为什么?我该如何解决这个问题

代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.List;

public class Temp {
    private static final WebDriver browser = new ChromeDriver();
    private static WebDriver temp_browser = new ChromeDriver();

    /*The code fails only for this url.*/
    private static String url = "https://sfbay.craigslist.org/pen/apa/5764613878.html";

    public static String phone_btns_xpath = "//section[@id='postingbody']//*[contains(.,'show contact info')]";
    public static By phone_btns_loc = By.xpath(phone_btns_xpath);

    public static void main(String[] args) {
        browser.get(url);
        List<String> phones = reveal_hidden_phone_numbers(temp_browser);
        temp_browser.close();
        System.out.println(phones);
    }

    public static List<String> reveal_hidden_phone_numbers(WebDriver temp_browser) {
        List<WebElement> phone_btns = browser.findElements(phone_btns_loc);
        List<String> phones = null;
        String text = null;

        if (phone_btns.size() > 0) {
            WebElement phone_btn_0 = phone_btns.get(0);
            System.out.println(phone_btn_0.getAttribute("innerHTML"));

            String url = phone_btn_0.getAttribute("href");
            temp_browser.get(url);
            text = temp_browser.findElement(By.tagName("body")).getText();

            for (WebElement phone_btn : phone_btns) {
                phone_btn.click();
            }

            phones = extract_phone_numbers(text);
        }
        return phones;
    }

    public static List<String> extract_phone_numbers(String text) {
        List<String> output = new ArrayList<String>();
        output.add("PHONE ;)");
        return output;
    }

}
 <a href="/fb/sfo/apa/5764613878" class="showcontact" title="click to show contact info" rel="nofollow">show contact info</a>

Exception in thread "main" java.lang.NullPointerException: null value in entry: url=null
    at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:33)
    at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:39)
    at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:49)
    at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:70)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:316)
    at com.craigslist.Temp.reveal_hidden_phone_numbers(Temp.java:38)
    at com.craigslist.Temp.main(Temp.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
import org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入java.util.ArrayList;
导入java.util.List;
公共类临时工{
私有静态最终WebDriver浏览器=新的ChromeDriver();
私有静态WebDriver temp_browser=新ChromeDriver();
/*代码仅对此url失败*/
专用静态字符串url=”https://sfbay.craigslist.org/pen/apa/5764613878.html";
公共静态字符串phone_btns_xpath=“//section[@id='postingbody']//*[包含(,'show contact info')]”;
公共静态By phone\u btns\u loc=By.xpath(phone\u btns\u xpath);
公共静态void main(字符串[]args){
browser.get(url);
列出电话=显示隐藏电话号码(临时浏览器);
temp_browser.close();
System.out.println(电话);
}
公共静态列表显示隐藏的电话号码(WebDriver临时浏览器){
列出电话号码=browser.findElements(电话号码);
列表电话=空;
字符串文本=空;
如果(电话号码\u btns.size()>0){
WebElement phone\u btn\u 0=phone\u btns.get(0);
System.out.println(phone_btn_0.getAttribute(“innerHTML”);
字符串url=phone_btn_0.getAttribute(“href”);
临时浏览器获取(url);
text=temp_browser.findElement(按.tagName(“body”)).getText();
对于(WebElement phone\u btn:phone\u btn){
电话点击();
}
电话=提取电话号码(文本);
}
归还电话;
}
公共静态列表提取电话号码(字符串文本){
列表输出=新的ArrayList();
输出。添加(“PHONE;”);
返回输出;
}
}
堆栈跟踪:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.List;

public class Temp {
    private static final WebDriver browser = new ChromeDriver();
    private static WebDriver temp_browser = new ChromeDriver();

    /*The code fails only for this url.*/
    private static String url = "https://sfbay.craigslist.org/pen/apa/5764613878.html";

    public static String phone_btns_xpath = "//section[@id='postingbody']//*[contains(.,'show contact info')]";
    public static By phone_btns_loc = By.xpath(phone_btns_xpath);

    public static void main(String[] args) {
        browser.get(url);
        List<String> phones = reveal_hidden_phone_numbers(temp_browser);
        temp_browser.close();
        System.out.println(phones);
    }

    public static List<String> reveal_hidden_phone_numbers(WebDriver temp_browser) {
        List<WebElement> phone_btns = browser.findElements(phone_btns_loc);
        List<String> phones = null;
        String text = null;

        if (phone_btns.size() > 0) {
            WebElement phone_btn_0 = phone_btns.get(0);
            System.out.println(phone_btn_0.getAttribute("innerHTML"));

            String url = phone_btn_0.getAttribute("href");
            temp_browser.get(url);
            text = temp_browser.findElement(By.tagName("body")).getText();

            for (WebElement phone_btn : phone_btns) {
                phone_btn.click();
            }

            phones = extract_phone_numbers(text);
        }
        return phones;
    }

    public static List<String> extract_phone_numbers(String text) {
        List<String> output = new ArrayList<String>();
        output.add("PHONE ;)");
        return output;
    }

}
 <a href="/fb/sfo/apa/5764613878" class="showcontact" title="click to show contact info" rel="nofollow">show contact info</a>

Exception in thread "main" java.lang.NullPointerException: null value in entry: url=null
    at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:33)
    at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:39)
    at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:49)
    at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:70)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:316)
    at com.craigslist.Temp.reveal_hidden_phone_numbers(Temp.java:38)
    at com.craigslist.Temp.main(Temp.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

线程“main”java.lang.NullPointerException中的异常:条目url=null中的null值
在com.google.common.collect.collectPremissions.checkEntryNotNull(collectPremissions.java:33)
位于com.google.common.collect.SingletonImmutableBiMap。(SingletonImmutableBiMap.java:39)
位于com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:49)
位于com.google.common.collect.ImmutableMap.of(ImmutableMap.java:70)
位于org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:316)
在com.craigslist.Temp.discover\u hidden\u phone\u number(Temp.java:38)
位于com.craigslist.Temp.main(临时java:23)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:483)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

正如我在您提供的stacktrace中看到的,这一行
System.out.println(phone\u btn\u 0.getAttribute(“innerHTML”)从您的代码将
phone\u btn\u 0
元素的内部HTML打印为:-

这意味着您试图在错误的元素上获取
href
属性。它位于父元素上,而不是实际的链接元素上,
href
属性不存在,这就是为什么您会得到
null

假设您希望从此打印的链接元素
HTML
获取
href
属性值,那么您应该尝试获取
phone\u btn\u 0
子元素的
href
属性值,如下所示:-

WebElement phone_btn_0 = phone_btns.get(0);
System.out.println(phone_btn_0.getAttribute("innerHTML"));

String url = phone_btn_0.findElement(By.tagName("a")).getAttribute("href");
已编辑:-您还可以在
xpath
中对其进行初始修复,以仅定位
a
元素,而不是使用相同代码定位所有
*
,以及:-

public static String phone_btns_xpath = "//section[@id='postingbody']//a[contains(.,'show contact info')]";

您可以使用下面的.to字符串方法..它对我有用
字符串url=phone_btn_0.findElement(按.tagName(“a”)).getAttribute(“href”).toString()

System.out.println(phone_btn_0.getAttribute(“innerHTML”))做了什么;输出?请尝试以下操作以查看哪些属性可用。System.out.println(((JavascriptExecutor)browser).executeScript(“返回参数[0].attributes”,phone_btn_0));元素);或者,我可以修改xpath以查找“a”而不是*或所有元素。我想我是对的。是的,你是对的,你也可以在xpath中首先修复它,只定位一个而不是
*
作为
公共静态字符串电话\u btns\u xpath=“//section[@id='postingbody']//a[包含(,'show contact info')”