Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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登录会话错误:TimeoutException:消息:_Python_Selenium_Selenium Webdriver_Web Scraping_Phantomjs - Fatal编程技术网

python selenium登录会话错误:TimeoutException:消息:

python selenium登录会话错误:TimeoutException:消息:,python,selenium,selenium-webdriver,web-scraping,phantomjs,Python,Selenium,Selenium Webdriver,Web Scraping,Phantomjs,我尝试使用python selenium登录,以下是我的代码: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait #as wait from selenium.webdriver.common.by import By from selenium.webdriver.supp

我尝试使用python selenium登录,以下是我的代码:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait #as wait
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import TimeoutException
option = webdriver.ChromeOptions()
option.add_argument(“ — incognito”)
decanter = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=option)
BASE_URL = 'www.decanter.com/wine-reviews/search#order[updated_at]=desc&page={0}'
decanter.get("http://"+BASE_URL.format(1))
delay_sec = 1
decanter.find_element_by_css_selector("button.secondary").click()
在这里之前,一切正常,上面的最后一行代码打开了弹出的登录窗口,如这里的屏幕截图所示:

我试图登录的以下代码遇到了“TimeoutException:Message:”错误

我已经试过了,它抛出了同样的错误。等待时间和路径也不是问题所在,我是肯定的

进一步的尝试和错误消息如下:

>>> WebDriverWait(decanter, delay_sec).until(ec.element_to_be_clickable((By.XPATH, "//label[@class='inputlabel' and contains(.,'E-mail')]//following::p[1]/input[@type='text']"))).send_keys(USER)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sheng/anaconda/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
>>WebDriverWait(倾析器,延迟秒)。直到(ec.element可点击((By.XPATH,“//label[@class='inputlabel'并包含(,'E-mail')]//following::p[1]/input[@type='text'])。发送密钥(用户)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Users/sheng/anaconda/lib/python2.7/site packages/selenium/webdriver/support/wait.py”,第80行,直到
引发TimeoutException(消息、屏幕、堆栈跟踪)
selenium.common.Exception.TimeoutException:消息:

您的定位器有点不正确。下面的代码应该可以工作

wait = WebDriverWait(decanter, delay_sec)
wait.until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"iframe[id^='piano-id-']"))
wait.until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input[fieldloginemail]'))).send_keys(USER)
decanter.find_element_by_css_selector('input[fieldloginpassword]').send_keys(PASSWORD)
decanter.find_element_by_css_selector('button[actionlogin]').click()
# once you are done with the content inside the iframe, switch context back to default
decanter.switch_to.default_content()

注意:将XPath用于多个级别不是一个好主意,尤其是那些以HTML标记开头的级别。即使对DOM进行了很小的更改,它们也很可能会中断。

当您试图将文本发送到userid和passwd字段时,您需要使用以下子句,而不是expectedconditions子句作为位于的元素的可见性子句:

USER = "userid"
PASSWORD = "passwd"
WebDriverWait(decanter, delay_sec).until(ec.element_to_be_clickable((By.XPATH, "//label[@class='inputlabel' and contains(.,'E-mail')]//following::p[1]/input[@type='text']"))).send_keys(USER)
decanter.find_element_by_xpath("//p[@class='input-group']/input[@type='password']").send_keys(PASSWORD)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"piano-id-XSavU"))
USER = "userid"
PASSWORD = "passwd"
WebDriverWait(decanter, delay_sec).until(ec.element_to_be_clickable((By.XPATH, "//current-screen/screen-login//label[@class='inputlabel' and contains(.,'E-mail')]//following::p[1]/input[@type='text']"))).send_keys(USER)
decanter.find_element_by_xpath("//p[@class='input-group']/input[@type='password']").send_keys(PASSWORD)
注意:在遍历DOM时是否使用CSS/XPath没有最佳实践。CSS和XPath都有各自的优缺点


更新 正如我在评论中提到的,根据您更新的HTML快照,元素位于
标记中。因此,您必须切换到预期的帧,如下所示:

USER = "userid"
PASSWORD = "passwd"
WebDriverWait(decanter, delay_sec).until(ec.element_to_be_clickable((By.XPATH, "//label[@class='inputlabel' and contains(.,'E-mail')]//following::p[1]/input[@type='text']"))).send_keys(USER)
decanter.find_element_by_xpath("//p[@class='input-group']/input[@type='password']").send_keys(PASSWORD)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"piano-id-XSavU"))
USER = "userid"
PASSWORD = "passwd"
WebDriverWait(decanter, delay_sec).until(ec.element_to_be_clickable((By.XPATH, "//current-screen/screen-login//label[@class='inputlabel' and contains(.,'E-mail')]//following::p[1]/input[@type='text']"))).send_keys(USER)
decanter.find_element_by_xpath("//p[@class='input-group']/input[@type='password']").send_keys(PASSWORD)

感谢JeffC的及时评论!!我肯定你的代码是有道理的。。。但它仍然返回相同的错误。。。你认为我的代码中还有什么可能会疯狂…?我会把延迟时间提高到5秒,也许10秒。可能网站加载得很慢。谢谢@JeffC,但实际上我将其设置为20,每次看到对话框位于
IFRAME
中时,我都会看到加载完成。添加了用于切换到HTML和退出的代码。遍历HTML并交叉检查
标记是否在一个帧内,然后需要首先切换到所需的帧。使用基于文本的格式化HTML和错误堆栈跟踪更新问题,以便进一步分析。我尝试了,但仍然是相同的错误消息。。。可能是因为当我看到受控屏幕时,光标已经在输入框中,在这种情况下,我还必须切换到它吗?谢谢,但在第一行代码之后仍然是相同的错误。