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