2016-09-05 5 views
1

要約:私のコードは、craigslistの広告URLに移動します。広告本文の非表示の電話番号を抽出します。コードは、コードに含まれているURL以外の多くのURLでうまく動作します。 (ところで、あなたは、任意の追加のコードを記述せずに自分のコードをコピーして実行することができます。)セレンのgetAttribute( "href")が機能しないのはなぜですか?

問題:getAttribute("href")のみこのURLの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) 
+0

System.out.println(phone_btn_0.getAttribute( "innerHTML"));出力?利用可能な属性を確認するには、次の手順を試してください。 System.out.println(((JavascriptExecutor)ブラウザ).executeScript( "戻り引数[0] .attributes"、phone_btn_0)); 要素); – MikeJRamsey56

答えて

1

私はあなたの提供スタックトレースで見ているように、このラインSystem.out.println(phone_btn_0.getAttribute("innerHTML"));あなたのコードからは、としてphone_btn_0要素の内部HTMLを印刷しました: -

あなたは間違った要素に href属性を取得しようとしている意味

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

。それはhref属性が存在しない実際のリンク要素の代わりに親要素上にあるため、nullを取得しています。

あなたは以下のようにphone_btn_0の子要素にhref属性値を取得しようとする必要がありますので、この印刷されたリンク要素HTMLから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"); 

編集: -

public static String phone_btns_xpath = "//section[@id='postingbody']//a[contains(.,'show contact info')]"; 
- :あなたはまた、唯一の a代わりに同じコードを持つすべての *の要素だけでなく、を見つけるために xpathで最初にそれを修正することができます
+1

または、xパスを修正して、*またはすべての要素の代わりに 'a'を探すことができます。私はそれが正しいと思った。 – testerjoe2

+0

はい、そうですが、最初にxpathで修正して、 '*'の代わりに '' public static String phone_btns_xpath = "// section [@ id = 'postingbody'] //を見つけることもできます。 。、 '連絡先情報を表示')] " –

0

私のために働く以下のような.to文字列メソッドを使用できます String url = phone_btn_0.findElement(By.tagName( "a"))。getAttribute( "href")。toString();

関連する問題