Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 硒+;Chrome WebDriver没有';t返回页面\ u源_Python 3.x_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python 3.x 硒+;Chrome WebDriver没有';t返回页面\ u源

Python 3.x 硒+;Chrome WebDriver没有';t返回页面\ u源,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在尝试使用selenium为Semantic Scholar改编一个webscraping脚本。这部分代码按预期工作,打开网站,在搜索栏中键入标题,然后按enter键 def _search_paper_by_name(self, paper_title: str) -> None: """ Go to the search page for 'paper_name'. """ self._w

我正在尝试使用selenium为Semantic Scholar改编一个webscraping脚本。这部分代码按预期工作,打开网站,在搜索栏中键入标题,然后按enter键

def _search_paper_by_name(self, paper_title: str) -> None:
    """
    Go to the search page for 'paper_name'.
    """

    self._web_driver.get(self._site_url)
    self._wait_element_by_name('q') 

    input_search_box = self._web_driver.find_element_by_name('q')
    input_search_box.send_keys(paper_title)
    input_search_box.send_keys(Keys.ENTER)
但是,如果在任何时候我尝试打印页面的代码,代码将按预期停止工作(不会在搜索栏上键入任何内容),并且不会打印HTML:

def _search_paper_by_name(self, paper_title: str) -> None:
    """
    Go to the search page for 'paper_name'.
    """

    self._web_driver.get(self._site_url)
    self._wait_element_by_name('q') 
    print(self._web_driver.page_source)

    input_search_box = self._web_driver.find_element_by_name('q')
    input_search_box.send_keys(paper_title)
    input_search_box.send_keys(Keys.ENTER)
我很困惑,我不明白为什么我不能打印源代码,为什么print语句会把其余的代码弄乱

编辑:按要求

我有一个SemanticScraper对象。创建对象时,web_驱动程序将初始化为无:

definit(self,timeout=5,调用之间的时间=0.5,headless=True, 网站url=https://www.semanticscholar.org/'): """ :param timeout:web驱动程序在引发超时错误之前应等待的秒数。 :参数api调用之间的时间:api调用之间的时间(以秒为单位)。 :param headless:如果设置为true,web驱动程序将以静默模式启动浏览器(chrome)。 :param site_url:semantic scholar搜索引擎的主页。 “”“

然后,每当刮板进行搜索时,它都会打开浏览器,如下所示:

def _start_browser(self):
    chrome_options = Options()
    if self._headless:
        chrome_options.add_argument("--headless")

    self._web_driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe')

您可以在定义了
self.\u web\u driver
的位置添加代码吗?运行此代码时出现的错误是什么?添加了请求的代码!非常感谢。我没有发现任何错误,它只是停止了预期的工作
def _start_browser(self):
    chrome_options = Options()
    if self._headless:
        chrome_options.add_argument("--headless")

    self._web_driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe')