Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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 Selenium不工作,除非启动浏览器并打开页面_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Selenium不工作,除非启动浏览器并打开页面

Python Selenium不工作,除非启动浏览器并打开页面,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我发现我的selenium除了启动Chrome之外无法执行代码 我不知道为什么我的硒不起作用。它只是用URL打开浏览器(Chrome),然后不做任何事情,甚至不最大化窗口,甚至不插入表单 我的代码有什么问题吗: from selenium import webdriver from selenium.webdriver.common.keys import Keys import re, time, csv driver = webdriver.Chrome("C:\\Users\\Ashra

我发现我的selenium除了启动Chrome之外无法执行代码

我不知道为什么我的硒不起作用。它只是用URL打开浏览器(Chrome),然后不做任何事情,甚至不最大化窗口,甚至不插入表单

我的代码有什么问题吗:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re, time, csv

driver = webdriver.Chrome("C:\\Users\\Ashraf%20Misran\\Installer\\chromedriver.exe")

driver.get("file:///C:/Users/Ashraf%20Misran/Devs/project-html/learning-html/selenium sandbox.html")
driver.maximize_window()
username = driver.find_element_by_xpath(".//input")
username.click()
username.send_keys("000200020002")
我打开的页面编码如下:

<!DOCTYPE html>
<html>
  <head>
    <title>Sandbox</title>
  </head>
  <body>
    <form>
      <input type="text" name="username">
    </form>
  </body>
</html>

沙箱

我认为问题出在网页上,您正试图打开它。建议先用简单的测试试试,比如打开谷歌页面,在搜索栏中输入一些东西。有了它,您将能够验证是否正确实现了驱动程序初始化


更新:尝试使用此css选择器:输入[name='username'],如果页面加载正确,则您的web元素选择器有问题。

我认为,使用相对xpath定位器有问题。请试一试:

username = driver.findElement(By.xpath("//input"))

您是否尝试过类似的方法,如just get google.com?尝试从文件名
selenium sandbox.html
->
selenium\u sandbox.html
页面已成功打开,如问题中所述。。。但不执行其他命令。这通常意味着驱动程序和浏览器版本不兼容。请确保完全更新浏览器,下载当前版本的Selenium和当前浏览器驱动程序,然后重试。发布您试图打开的页面,这可能会有所帮助页面已成功打开,如问题中所述。。。但是没有执行其他命令。我更新了我的答案。看起来,您的xpath不正确。我写了一个css选择器的例子,它应该可以工作。此外,控制台中是否显示任何消息/错误?我认为应该是什么,什么可以帮助解决这个问题。