调用 ChromeDriver.Navigate() 时 C# selenium SocketException?
C# selenium SocketException when calling ChromeDriver.Navigate()?
我开始使用 Selenium 的时间并不长。我正在编写一个 WPF 应用程序,它应该自动在此站点上查找一本书,然后在 ListView 上表示结果。一开始一切都很好,当我决定第二次搜索时,问题就出现了。每次按下搜索按钮时,我都会调用 WebDriver 的 Navigate().GoToUrl() 方法,在第一次之后,如果我再次按下它,我会得到这个:
OpenQA.Selenium.WebDriverException: 'Unexpected error. System.Net.WebException: Impossible to connect with the remote server. ---> System.Net.Sockets.SocketException: Impossible to estabilish a connection. Persistent refusal of the computer 127.0.0.1:52601
in System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
in System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- Fine della traccia dello stack dell'eccezione interna ---
in System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
in System.Net.HttpWebRequest.GetRequestStream()
in OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
in OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
那是我的主窗口 class,一切都在这里完成:
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Xml;
using System.IO;
using System.Net;
using System.Windows.Controls;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Threading;
namespace MyLibrary
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Catalog catalog;
List<Category> categories;
XmlDocument catalogXml;
string xmlPath;
ChromeDriver resultFinder;
Thread resultFinderThread;
string searchName;
struct ResultViewListItem
{
public string N { get; set; }
public string Result { get; set; }
}
public MainWindow()
{
InitializeComponent();
LoadCatalog();
LoadResultFinder();
}
private void LoadCatalog()
{
categories = new List<Category>();
xmlPath = "C:/Users/Andrea Ferrando/Documents/Visual Studio 2017/Projects/Andrea/MyLibrary/MyLibrary/Catalog/catalog.xml";
catalogXml = new XmlDocument();
catalogXml.Load(xmlPath);
foreach(XmlNode node in catalogXml.DocumentElement.ChildNodes)
{
categories = getCategories(categories, node);
}
catalog = new Catalog(categories, catalogXml, xmlPath);
}
private List<Category> getCategories(List<Category> categories, XmlNode node)
{
int number = Int32.Parse(node.Attributes["number"].Value);
string description = node.Attributes["description"].Value;
Category category = new Category(number, description, categories);
categories.Add(category);
if(node.HasChildNodes)
{
foreach (XmlNode child in node.ChildNodes)
{
category.Children = getCategories(category.Children, child);
}
return categories;
}
return categories;
}
private void LoadResultFinder()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--window-position=-32000,-32000");
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
resultFinder = new ChromeDriver(chromeDriverService, chromeOptions);
searchName = "";
}
private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if(e.Source.Equals(FindName("Search")))
{
TextBox textBox = (TextBox)FindName("SearchBox");
searchName = textBox.Text;
resultFinderThread = new Thread(new ThreadStart(findResults));
resultFinderThread.Start();
}
}
private void findResults()
{
using (resultFinder)
{
resultFinder.Navigate().GoToUrl("http://www.sbn.it/opacsbn/opac/iccu/free.jsp");
IWebElement inputField = resultFinder.FindElementByName("item:1016:Any");
inputField.SendKeys(searchName);
IWebElement submitField = resultFinder.FindElementByName("Invia");
submitField.Click();
IWebElement table = resultFinder.FindElementById("records");
IList<IWebElement> trElements = table.FindElements(By.TagName("tr"));
IList<IWebElement> tdElements;
List<string> information = new List<string>();
foreach (IWebElement trElement in trElements)
{
tdElements = trElement.FindElements(By.TagName("td"));
information.Add(tdElements[2].Text);
}
Dispatcher.Invoke(() =>
{
ListView listView = (ListView)FindName("ResultsList");
for (int i = 0; i < information.Count; i++)
{
listView.Items.Add(new ResultViewListItem { N = (i + 1).ToString(), Result = information[i] });
}
});
}
}
}
}
重要的方法是FindResults()
我希望我已经足够清楚了,提前致谢。
我猜你的连接在你第二次调用它时不知何故关闭了。
我猜你也调用了两次方法 findResults()
。
问题是您正在使用 using
语句。一旦退出代码块,此语句就会处理连接。在 this 答案中,您可以找到有关其工作原理的更多信息。
您不应该在使用完对象之前处置该对象。我会删除 using
开始,然后我会使用其他一些技术来处理对象。
希望这对您有所帮助。
我开始使用 Selenium 的时间并不长。我正在编写一个 WPF 应用程序,它应该自动在此站点上查找一本书,然后在 ListView 上表示结果。一开始一切都很好,当我决定第二次搜索时,问题就出现了。每次按下搜索按钮时,我都会调用 WebDriver 的 Navigate().GoToUrl() 方法,在第一次之后,如果我再次按下它,我会得到这个:
OpenQA.Selenium.WebDriverException: 'Unexpected error. System.Net.WebException: Impossible to connect with the remote server. ---> System.Net.Sockets.SocketException: Impossible to estabilish a connection. Persistent refusal of the computer 127.0.0.1:52601
in System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
in System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- Fine della traccia dello stack dell'eccezione interna ---
in System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
in System.Net.HttpWebRequest.GetRequestStream()
in OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
in OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
那是我的主窗口 class,一切都在这里完成:
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Xml;
using System.IO;
using System.Net;
using System.Windows.Controls;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Threading;
namespace MyLibrary
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Catalog catalog;
List<Category> categories;
XmlDocument catalogXml;
string xmlPath;
ChromeDriver resultFinder;
Thread resultFinderThread;
string searchName;
struct ResultViewListItem
{
public string N { get; set; }
public string Result { get; set; }
}
public MainWindow()
{
InitializeComponent();
LoadCatalog();
LoadResultFinder();
}
private void LoadCatalog()
{
categories = new List<Category>();
xmlPath = "C:/Users/Andrea Ferrando/Documents/Visual Studio 2017/Projects/Andrea/MyLibrary/MyLibrary/Catalog/catalog.xml";
catalogXml = new XmlDocument();
catalogXml.Load(xmlPath);
foreach(XmlNode node in catalogXml.DocumentElement.ChildNodes)
{
categories = getCategories(categories, node);
}
catalog = new Catalog(categories, catalogXml, xmlPath);
}
private List<Category> getCategories(List<Category> categories, XmlNode node)
{
int number = Int32.Parse(node.Attributes["number"].Value);
string description = node.Attributes["description"].Value;
Category category = new Category(number, description, categories);
categories.Add(category);
if(node.HasChildNodes)
{
foreach (XmlNode child in node.ChildNodes)
{
category.Children = getCategories(category.Children, child);
}
return categories;
}
return categories;
}
private void LoadResultFinder()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--window-position=-32000,-32000");
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
resultFinder = new ChromeDriver(chromeDriverService, chromeOptions);
searchName = "";
}
private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if(e.Source.Equals(FindName("Search")))
{
TextBox textBox = (TextBox)FindName("SearchBox");
searchName = textBox.Text;
resultFinderThread = new Thread(new ThreadStart(findResults));
resultFinderThread.Start();
}
}
private void findResults()
{
using (resultFinder)
{
resultFinder.Navigate().GoToUrl("http://www.sbn.it/opacsbn/opac/iccu/free.jsp");
IWebElement inputField = resultFinder.FindElementByName("item:1016:Any");
inputField.SendKeys(searchName);
IWebElement submitField = resultFinder.FindElementByName("Invia");
submitField.Click();
IWebElement table = resultFinder.FindElementById("records");
IList<IWebElement> trElements = table.FindElements(By.TagName("tr"));
IList<IWebElement> tdElements;
List<string> information = new List<string>();
foreach (IWebElement trElement in trElements)
{
tdElements = trElement.FindElements(By.TagName("td"));
information.Add(tdElements[2].Text);
}
Dispatcher.Invoke(() =>
{
ListView listView = (ListView)FindName("ResultsList");
for (int i = 0; i < information.Count; i++)
{
listView.Items.Add(new ResultViewListItem { N = (i + 1).ToString(), Result = information[i] });
}
});
}
}
}
}
重要的方法是FindResults() 我希望我已经足够清楚了,提前致谢。
我猜你的连接在你第二次调用它时不知何故关闭了。
我猜你也调用了两次方法 findResults()
。
问题是您正在使用 using
语句。一旦退出代码块,此语句就会处理连接。在 this 答案中,您可以找到有关其工作原理的更多信息。
您不应该在使用完对象之前处置该对象。我会删除 using
开始,然后我会使用其他一些技术来处理对象。
希望这对您有所帮助。