我已成功运行一个脚本来读取蛋白质登记号列表并将其发送到网站以获取其名称。我意识到使用driver.close();将chrome作为一个进程运行并最终导致eclipse崩溃。我切换到driver.quit();并且它正常工作,直到它出现DNS查找错误。在eclipse运行缓慢之后,现在当我运行代码时,它会打开网页,按下搜索,并且不会发送任何键或按任何按钮。其他项目仍然很好。我尝试重启几次并清除.history文件夹,但这些都没有解决问题。
是否有人遇到此问题或知道如何更正?
代码:
package ncbi;
import java.io.BufferedReader;//and all other necessary imports
public class NameGetter {
static int z = 0;
static String b;
public static void main(String[] args) throws IOException {
nameFromNCBI();
}
private static void nameFromNCBI() throws IOException {
String names;
String names2;
int i = 0;
int a = 0;
File file = new File("subjectNames4.txt");
file.createNewFile();
while (a < 26405) {
b = Integer.toString(z);
BufferedReader reader = new BufferedReader(new FileReader("X:\\FilePath" + b + ".txt")); //this is to handle errors to load a new file from the line it left off from
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
while ((names = reader.readLine()) != null) {
System.setProperty("webdriver.chrome.driver", "X:\\Java\\Selenium\\chromeAPI\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.ncbi.nlm.nih.gov/protein/");
WebElement searchTerm = driver.findElement(By.id("term"));
searchTerm.sendKeys(names);
WebElement press = driver.findElement(By.id("search"));
press.click();
WebElement element = (new WebDriverWait(driver, 600)).until(ExpectedConditions.elementToBeClickable(By.className("dblinks")));
WebElement theName = driver.findElement(By.xpath("//*[@id='maincontent']/div/div[5]/div[1]/h1"));
writer.write(theName.getText());
System.out.println(theName.getText());
writer.write("\r\n");
driver.quit();
a++;
}
}
catch (Exception e) {
z++;
b = Integer.toString(z);
File file2 = new File("queryNames"+ b + ".txt");
file2.createNewFile();
i = 0;
System.out.println(a);
BufferedReader reader2 = new BufferedReader(new FileReader("X:\\FilePath"));
try (BufferedWriter writer2 = new BufferedWriter( new FileWriter(file2, true))) {
while ((names2 = reader2.readLine()) != null) {
if (i < a) {
i++;
}
else if (i >= a) {
writer2.write(names2);
writer2.write("\r\n");
i++;
}
}
}
reader2.close();
}
reader.close();
}
}
}