我尝试在网页上按Xpath查找元素并将其存储为字符串以便稍后使用。 我在这堆叠了。 我认为我的代码没有做正确的事情。它打开Url进行我需要的搜索但不存储任何元素,或者它只是根本找不到它
你可以帮我解决一下吗? 谢谢!我的代码:
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;
import org.json.XML;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.Uiproject.flickr.APIprop;
public class renaming {
public static void main(String[] args) {
WebDriver driver;
String baseUrl;
Client client;
client = ClientBuilder.newClient();
String FlickrUrl = APIprop.flickrurl;
Invocation.Builder invocationBuilder = client.target(FlickrUrl)
.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();
String content = response.readEntity(String.class);
System.out.println(content);
JSONObject jsonresponse = XML.toJSONObject(content)
.getJSONObject("rsp").getJSONObject("photos")
.getJSONObject("photo");
String title = jsonresponse.getString("title");
driver = new FirefoxDriver();
baseUrl = "http://www.airliners.net";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl + "/");
driver.findElement(By.id("q")).click();
driver.findElement(By.id("q")).clear();
driver.findElement(By.id("q")).sendKeys(title);
driver.findElement(By.name("submit")).click();
new Select(driver.findElement(By.name("sort_order")))
.selectByVisibleText("Latest Additions First");
driver.findElement(By.cssSelector("option[value=\"photo_id desc\"]"))
.click();
List<WebElement> resultList = driver
.findElements(By
.xpath(".//html/body/div/div[3]/div/table[2]/tbody/tr[2]/td[1]/div[1]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/table/tbody/tr/td/font/a[1]"));
for (WebElement resultItem : resultList) {
String tabname = resultItem.getText();
System.out.println(tabname);
}
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.quit();
}
}
问题出在Xpath上
正确的一个如下:
//div[@id='content']/div/table[2]/tbody/tr[2]/td/div/table/tbody/tr/td/table/tbody/tr[2]/td[2]
谢谢大家!