[英]selenium.common.exceptions.NoSuchElementException from dynamic content
There is a form on this site and I am trying to get every option from it and click a search button with this code: 该站点上有一个表单 ,我正在尝试从中获取所有选项,然后单击带有以下代码的搜索按钮:
from selenium import webdriver
driver = webdriver.PhantomJS('c:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
driver.get('http://www.cobar.org/Find-A-Lawyer')
driver.implicitly_wait(20)
options = driver.find_element_by_xpath('//select[@id="FAL_FOP_field"]')
for option in options.find_elements_by_tag_name('option'):
if option.text != 'ALL':
option.click()
#click search button
driver.find_element_by_xpath('//button[@class="btn btn-primary btn-main"]').click()
lawyer = driver.find_element_by_xpath('//table[@id="myTable"]/tbody/tr/td[0]')
print(lawyer)
However I am getting: 但是我得到:
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":
"Unable to find element with xpath '//select[@id=\"FAL_FOP_field\"]'",
"request":{"headers":{"Accept":"application/json","Accept-Encoding":
"identity","Connection":"close","Content-Length":"115","Content-Type":
"application/json;charset=UTF-8","Host":"127.0.0.1:52809","User-Agent":"Python-urllib/2.7"}
,"httpVersion":"1.1","method":"POST","post":
"{\"using\": \"xpath\", \"sessionId\": \"e03be070-e353-11e6-83b5-5f7f74696cce\","
" \"value\": \"//select[@id=\\\"FAL_FOP_field\\\"]\"}","url":"/element",
"urlParsed":{"anchor":"","query":"","file":"element","directory":"/",
"path":"/element","relative":"/element","port":"","host":"","password":"","user":"",
"userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},
"chunks":["element"]},"urlOriginal":"/session/e03be070-e353-11e6-83b5-5f7f74696cce/element"}}
what should I do? 我该怎么办?
Error is because fields you are locating are inside iFrame . 错误是因为您要查找的字段位于iFrame内部。 So, first you need to switch to iframe and then locate your elements.
因此,首先需要切换到iframe,然后找到您的元素。 still, if didn't work, add time delay.
仍然,如果不起作用,请增加时间延迟。
To locate iFrame : 找到iFrame:
WebElement iframelocator = driver.findElement(By.xpath("//iframe[@id='dnn_ctr2047_IFrame_htmIFrame']"));
Then Switch to iFrame 然后切换到iFrame
driver.switchTo().frame(iframelocator);
Add above two steps in your code before locating elements. 查找元素之前,在代码中添加以上两个步骤。
Note : Above written code is in java. 注意 :上面编写的代码在java中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.