1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
問題似乎與您的 for 循環(huán)結(jié)構(gòu)有關(guān)。首先,關(guān)于一些觀察的幾個(gè)注意事項(xiàng)。您需要在這里進(jìn)行高隱性等待,因?yàn)樵撜军c(diǎn)加載結(jié)果的速度很慢。你的行 xpath 沒有找到任何東西,但你的列 xpath 有效,即使當(dāng)我移植到 Protactor 時(shí)它沒有。我正在草擬一個(gè)替代方案,使用 id "f1" 的定位器并拆分結(jié)果文本,但事實(shí)證明這等同于你的 xpath "column" 找到的內(nèi)容。這里的關(guān)鍵是要知道新行何時(shí)開始以及這些項(xiàng)目何時(shí)不再是您真正想要的數(shù)據(jù)。當(dāng)索引是 3 的倍數(shù)時(shí),行開始,因?yàn)槊啃杏?3 個(gè)字段(一個(gè)名稱和 2 個(gè)數(shù)字)。我們不關(guān)心的內(nèi)容以數(shù)字 1 開頭。我沒有編寫代碼將數(shù)據(jù)放入 Excel 中,但我指出了您要放置的位置。
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ShonaDriver {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "bin/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(
"https://careernavigator.naukri.com/sales-executive-retail-careers-in-mahindra-and-mahindra-financial-services-15731");
/// the row xpath that ws once here found nothing
List<WebElement> column = driver.findElements(
By.xpath("(//*[name()='svg'])[2]//*[name()='text']//*[name()='tspan' and (@dy=4 or @dy='3.5')]"));
int spreadSheetRowNum = 0;
int spreadSheetColumnNum = 0;
WebElement f1 = driver.findElement(By.id("f1"));
for (int i = 0; i < column.size(); i++) {
if (column.get(i).getText().equalsIgnoreCase("1")) {
// reached end of meaningful fields
break;
}
if (i % 3 == 0) {// start of new row
spreadSheetRowNum++;
System.out.println("here create row: " + spreadSheetRowNum);
spreadSheetColumnNum = 1;// assuming excel column A is column 1
} else {
spreadSheetColumnNum++;
}
System.out.println("for column list " + i + " text is:");
System.out.println(column.get(i).getText());
System.out.println("write it to row " + spreadSheetRowNum + " column " + spreadSheetColumnNum);
}
String[] f1Arr = f1.getText().split("\n");
System.out.println("if you prefer to use the f1 array, its contents are:");
for (int i = 0; i < f1Arr.length; i++) {
System.out.println("f1[" + i + "] = " + f1Arr[i]);
}
driver.close();
}
}
這是我得到的輸出,指示那里有什么以及您需要在各個(gè)步驟中做什么:
here create row: 1
for column list 0 text is:
Mahindra and Mahindra Financia..
write it to row 1 column 1
for column list 1 text is:
4.8
write it to row 1 column 2
for column list 2 text is:
2.4
write it to row 1 column 3
here create row: 2
for column list 3 text is:
Tata Motors
write it to row 2 column 1
for column list 4 text is:
5.0
write it to row 2 column 2
for column list 5 text is:
2.6
write it to row 2 column 3
here create row: 3
for column list 6 text is:
Mahindra and Mahindra
write it to row 3 column 1
for column list 7 text is:
4.6
write it to row 3 column 2
for column list 8 text is:
2.9
write it to row 3 column 3
if you prefer to use the f1 array, its contents are:
f1[0] = Mahindra and Mahindra Financia..
f1[1] = 4.8
f1[2] = 2.4
f1[3] = Tata Motors
f1[4] = 5.0
f1[5] = 2.6
f1[6] = Mahindra and Mahindra
f1[7] = 4.6
f1[8] = 2.9
f1[9] = 1
f1[10] = 1
f1[11] = 2
f1[12] = 2
f1[13] = 3
f1[14] = 3
f1[15] = 4
f1[16] = 4
f1[17] = 5
f1[18] = 5
f1[19] = 6
f1[20] = 6
f1[21] = 7
f1[22] = 7
f1[23] = 8
f1[24] = 8
f1[25] = Avg.Exp
f1[26] = Avg.Sal
f1[27] = In lacs
f1[28] = Avg.Exp
f1[29] = Avg.Sal
f1[30] = In lacs
f1[31] = Top Companies
f1[32] = Top Companies
f1[33] = Top Companies
f1[34] = Top Companies
f1[35] = 1.6
f1[36] = 4.9
f1[37] = 2.4
f1[38] = 1.6
f1[39] = 7.0
f1[40] = 2.6
f1[41] = 1.3
f1[42] = 7.2
f1[43] = 2.9
f1[44] = View 22 more Companies.
添加回答
舉報(bào)