- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试以编程方式在网站中选择一种货币:http://www.asos.com/asos/asos-skinny-chinos-in-dark-khaki/prd/5542109
但是它给我带来了一些问题,我正在使用以下代码:
def set_currency(text):
one = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "selected-currency")))
one.click()
select_element = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
select = Select(select_element)
select.select_by_visible_text(text)
有时它会给我这个错误:selenium.common.exceptions.ElementNotVisibleException:
在这一行select.select_by_visible_text(text)
但有时它工作得很好。我正在使用 WebDriverWait
直到它可见,所以我不明白为什么它犯了这个错误。
如果需要的话,完整错误:
Traceback (most recent call last):
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 35, in <module>
set_currency(currency)
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 18, in set_currency
select.select_by_visible_text(text)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\select.py", line 120, in select_by_visible_text
self._setSelected(opt)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
option.click()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:17758","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST","post":"{\"id\": \":wdc:1479563250968\", \"sessionId\": \"b2aa4180-ae5e-11e6-b8b3-e1a4ad040bb7\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/b2aa4180-ae5e-11e6-b8b3-e1a4ad040bb7/element/:wdc:1479563250968/click"}}
Screenshot: available via screen
编辑:
当前代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import ElementNotVisibleException
import traceback
from selenium.webdriver.support.select import Select
driver1 = webdriver.PhantomJS(r'C:\Users\dodob\Desktop\Apps Workspace\phantomjs-2.1.1-windows\bin\phantomjs.exe')
def set_currency(label):
is_change_currency_displayed = driver1.find_element_by_id("currencyList").is_displayed()
if not is_change_currency_displayed:
print("dropdown is not displayed.")
one = WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
one.click()
select_element = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
select = Select(select_element)
select.select_by_visible_text(label)
def get_all_currencies():
one = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "selected-currency")))
one.click()
el = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
currency2 = []
options = el.find_elements_by_tag_name('option')
for option in options:
currency2.append(option.text)
return currency2
def main(url):
print(url)
driver1.get(url)
to_return_string = ''
list_of_currencies = get_all_currencies()
print(list_of_currencies)
for currency in list_of_currencies:
try:
set_currency(currency)
current_price = WebDriverWait(driver1, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, ".current-price")))
to_return_string += ("In " + currency + " : " + current_price.text + ' \n')
print("In", currency, ":", current_price.text)
except TimeoutException:
print(traceback.print_exc())
print("In", currency, ":", "Timed out waiting for page to load")
to_return_string += ("In " + currency + " : " + " Timed out waiting for page to load" + ' \n')
except ElementNotVisibleException:
print(traceback.print_exc())
return to_return_string
main('http://www.asos.com/it/asos/asos-jeans-skinny-alla-caviglia-kaki/prd/6759361')
电流输出:
C:\Python\Python35\python.exe C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py
http://www.asos.com/it/asos/asos-jeans-skinny-alla-caviglia-kaki/prd/6759361
['£ GBP', '$ USD', 'C$ CAD', 'kr SEK', 'kr NOK', 'kr DKK', '₣ CHF', ' € EUR', '$ AUD', '¥ RMB', '$ HKD', '$ NZD', '$ SGD', 'NT$ TWD', 'руб. RUB']
In £ GBP : € 33,99
In $ USD : € 33,99
None
Traceback (most recent call last):
In C$ CAD : Timed out waiting for page to load
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 43, in main
set_currency(currency)
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 19, in set_currency
select_element = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
dropdown is not displayed.
In kr SEK : € 33,99
In kr NOK : € 33,99
None
Traceback (most recent call last):
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 43, in main
set_currency(currency)
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 21, in set_currency
select.select_by_visible_text(label)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\select.py", line 120, in select_by_visible_text
self._setSelected(opt)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
option.click()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:60873","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST","post":"{\"id\": \":wdc:1479841552848\", \"sessionId\": \"aae52750-b0e6-11e6-a0c1-5193111f996c\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/aae52750-b0e6-11e6-a0c1-5193111f996c/element/:wdc:1479841552848/click"}}
Screenshot: available via screen
dropdown is not displayed.
In ₣ CHF : € 33,99
In € EUR : € 33,99
Traceback (most recent call last):
None
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 43, in main
set_currency(currency)
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 21, in set_currency
select.select_by_visible_text(label)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\select.py", line 120, in select_by_visible_text
self._setSelected(opt)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
option.click()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:60873","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST","post":"{\"id\": \":wdc:1479841552851\", \"sessionId\": \"aae52750-b0e6-11e6-a0c1-5193111f996c\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/aae52750-b0e6-11e6-a0c1-5193111f996c/element/:wdc:1479841552851/click"}}
Screenshot: available via screen
dropdown is not displayed.
In ¥ RMB : € 33,99
In $ HKD : € 33,99
None
In $ NZD : Timed out waiting for page to load
Traceback (most recent call last):
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 43, in main
set_currency(currency)
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 19, in set_currency
select_element = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
dropdown is not displayed.
In $ SGD : € 33,99
In NT$ TWD : € 33,99
None
Traceback (most recent call last):
In руб. RUB : Timed out waiting for page to load
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 43, in main
set_currency(currency)
File "C:/Users/dodob/PycharmProjects/AsosPriceCheckerWindows/currency.py", line 19, in set_currency
select_element = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Process finished with exit code 0
最佳答案
问题:
代码看起来没问题。但问题的根源在于调用的顺序。
当在 main
方法中调用以下行时:
list_of_currencies = get_all_currencies()
您正在 get_all_currencies() 方法的以下行中单击selected-currency
:
one.click() // opens the "Change-Currency" dropdown menu as shown in below image
并且该方法中没有采取进一步的操作。
然后我们在主方法的for循环
中调用set_currency(currency)
。
在set_currency(currency)
方法中,我们再次单击selected-currency
,这实际上是关闭Change-Currency
打开的下拉列表>,这会导致元素不可见异常。
解决方案:
因此,请更改流程以确保在调用 set_by_visible_text
方法之前打开 Change-Currency
下拉列表。
使用 isDisplayed
方法检查 Change-Currency
下拉列表是否已打开。
以下是代码:(在开头添加了2行)
def set_currency(label):
isChangeCurrencyDisplayed = driver.find_element_by_id("currencyList").is_displayed()
if not isChangeCurrencyDisplayed:
print "dropdown is not displayed."
one = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
one.click()
select_element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "currencyList")))
select = Select(select_element)
select.select_by_visible_text(text) # here text is "$ USD"
关于python - select_by_visible_text python selenium ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693700/
此页面的主菜单( linio )有 11 个链接。只对 9 感兴趣(那些具有灰色背景并且悬停时显示子菜单的)。 我想从 9 个选项中单击子菜单中的每个元素。期望的过程是: 1.-第一部分:“Celul
我正在使用 Java selenium 自动输入用户的数据,但他必须通过下拉列表选择状态。我收到异常错误。 我尝试过使用所有选择器: drpState.selectByIndex(5); drpSta
我已经查看了涉及该主题的一些其他问题,但我没有在任何地方看到这个特定问题。我有一个点击 Web 元素的测试。我尝试通过 ID 和 XPath 引用它,并使用 wait.until() 等待它变得可见。
我正在尝试扩展所有评论、回复,在评论中查看更多内容,并在 Facebook 的帖子中查看更多内容。 1)我在下面编写的代码允许我扩展我想要的所有内容,除了帖子中的一些回复,即使我已将回复按钮放在循环中
我使用 EBays 的高级上传工具(URL:http://cgi5.ebay.com/ws/eBayISAPI.dll?NewListing&cpg=20&aid=1&from=wn&sid=5297
我看到很多关于 Selenium 的 ElementNotVisibleException 问题的问题/答案。到目前为止我完成的最好的代码如下: from selenium.webdriver.sup
我正在尝试使用 webdriver 单击链接,但它向我抛出一个 ElementNotVisibleException 说“元素当前不可见,因此可能无法与之交互” 我的网络驱动程序代码: addProg
import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import o
我正在使用 Selenium 和 Python,并且我正在尝试填写登录表单,我已经成功填写用户名字段,但无法填写密码。这是 HTML:
当我试图点击一个按钮时出现以下错误: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible
我正在使用 Selenium Webdriver 登录网站。我尝试了多种不同的选择器,并尝试了隐式等待,但无法找到该元素。 from selenium import webdriver
我目前正在尝试使用 Selenium 进行一些用户界面测试,我遇到了这个很好的方法(不确定我从哪里得到它......),它应该处理不存在的元素和隐藏元素...... 问题在于第二个问题:即使元素未显示
我正在尝试使用我正在使用的 Selenium 进行黑盒测试: Mac 操作系统:版本:10.9.4 java.版本:1.6.0_65 浏览器:firefox 版本 32.0.3 Selenium :版
尝试从多选下拉框中进行选择,发现似乎是一个不错的资源。 我正在研究http://preview.harriscountyfws.org/我正在尝试在“按机构选择”多选下拉列表中选择多个项目。 我在这里
我想做的就是:转到“http://news.google.com”,然后单击侧面菜单上的“技术”链接。这是我的代码: from selenium import webdriver from selen
目前我有此输入 + Add certificate 并且我想将文件发送到同一地址,因此我运行以下代码: self.driver.execute_script("docu
我试图让 selenium 单击 li 元素内的链接。这是在 while 循环内发生的。点击一直有效,直到我到达 li 低于 Firefox 窗口的水平。如果我在 selenium 尝试点击之前在 F
我有一个帮助函数来浏览页面并打开它找到的每个帮助按钮。它既漂亮又简单; def openHelp(ff): """ Opens all the help buttons on the
我正在尝试以编程方式在网站中选择一种货币:http://www.asos.com/asos/asos-skinny-chinos-in-dark-khaki/prd/5542109 但是它给我带来了一
我从来没有认真研究过 HTML,所以我要说的可能是不对的。 当我编写 selenium 代码时,我注意到某些网页上的某些按钮不会重定向到其他页面,但它们会更改第一个页面的结构。据我了解,发生这种情况是
我是一名优秀的程序员,十分优秀!