存在元素时发生NoSuchElementException [英] NoSuchElementException when element is present
问题描述
这是我的代码:
@return_to_home()
def test_upvote(self):
driver.find_element_by_id("com.ATG.World:id/fab").click()
driver.implicitly_wait(timer)
driver.find_element_by_id("com.ATG.World:id/share_url_fab_clicked.").click()
driver.find_element_by_id("com.ATG.World:id/tvSelectGroup").click()
driver.find_element_by_id("com.ATG.World:id/tv_group_name").click()
driver.find_element_by_id("com.ATG.World:id/ibCLose").click()
driver.find_element_by_id("com.ATG.World:id/edtShareLink").send_keys("https://www.google.com")
driver.implicitly_wait(timer)
driver.find_element_by_id("com.ATG.World:id/ivShareUrl").click()
driver.find_element_by_id("com.ATG.World:id/tv_likes").is_enabled()
driver.find_element_by_id("com.ATG.World:id/tv_likes").click()
verify_api("ws-upvote")
try:
driver.find_element_by_id("com.ATG.World:id/post_details_back").click()
driver.implicitly_wait(timer)
driver.find_element_by_id("com.ATG.World:id/cross_fab_clicked").click()
except:
pass
@return_to_home()
def test_dislIke(self):
driver.find_element_by_id("com.ATG.World:id/fab").click()
driver.implicitly_wait(timer)
driver.find_element_by_id("com.ATG.World:id/share_url_fab_clicked.").click()
driver.find_element_by_id("com.ATG.World:id/tvSelectGroup").click()
driver.find_element_by_id("com.ATG.World:id/tv_group_name").click()
driver.find_element_by_id("com.ATG.World:id/ibCLose").click()
driver.find_element_by_id("com.ATG.World:id/edtShareLink").send_keys("https://www.google.com")
driver.implicitly_wait(timer)
driver.find_element_by_id("com.ATG.World:id/ivShareUrl").click()
driver.find_element_by_id("com.ATG.World:id/tv_unlikes").is_enabled()
driver.find_element_by_id("com.ATG.World:id/tv_unlikes").click()
try:
driver.find_element_by_id("com.ATG.World:id/post_details_back").click()
driver.find_element_by_id("com.ATG.World:id/touch_outside").click()
driver.find_element_by_id("com.ATG.World:id/cross_fab_clicked").click()
except:
pass
相同的代码有效,但不喜欢的方法无效 甚至在评论不同行之后
Same code is working but dislike method is not working Even after commenting unlike line
错误日志是
ERROR: test_dislIke (__main__.shareurl)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Vaishali\Desktop\atg_android_automation\Common\atg_common.py", line 48, in wrapper
func = orig_func(self, *args, **kwargs)
File "C:\Users\Vaishali\Desktop\atg_android_automation\TestSuite\ShareUrl.py", line 148, in test_dislIke
driver.find_element_by_id("com.ATG.World:id/fab").click()
File "C:\Users\Vaishali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Vaishali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\webdriver.py", line 284, in find_element
'value': value})['value']
File "C:\Users\Vaishali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Vaishali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
raise wde
File "C:\Users\Vaishali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
super(MobileErrorHandler, self).check_response(response)
File "C:\Users\Vaishali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.
推荐答案
首先,当您使用find_element_by_id
方法时,硒将通过使用[id="%s"]
将值转换为css(%s是您提供的值).
First of all, when you use find_element_by_id
method selenium will convert the value to css by using [id="%s"]
(%s is the value you provided).
现在,您在错误行中提供的com.ATG.World:id/fab
id不是有效的CSS.您必须使用\
屏蔽所有特殊字符,如下所示.
Now that the com.ATG.World:id/fab
id that you provided in the error line is not a valid css. You have to mask all the special chars with \
as shown below.
driver.find_element_by_id("com\.ATG\.World\:id\/fab").click()
下面是证据它是如何工作的.我更喜欢使用find_element_by_css_selector
并提供如选项1或2所示的完整CSS.
Below is the evidence how it works. I would prefer using find_element_by_css_selector
and provide the complete css as shown in either option 1 or 2.
选项1:
选项2:
这篇关于存在元素时发生NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!