&引用;元素未附加到页面文档";使用selenium和python时出错
我想在5个不同的文件中打印5个不同选项的值,因此我一直在尝试使用以下代码:&引用;元素未附加到页面文档";使用selenium和python时出错,python,selenium,Python,Selenium,我想在5个不同的文件中打印5个不同选项的值,因此我一直在尝试使用以下代码: driver=webdriver.Chrome() driver.get('url'); time.sleep(3) ele=driver.find_element_by_id("reportDate") ele.clear() ele.send_keys("21-Nov-2015") currency = driver.find_element_by_xpath("//*[@id='currencyCode']
driver=webdriver.Chrome()
driver.get('url');
time.sleep(3)
ele=driver.find_element_by_id("reportDate")
ele.clear()
ele.send_keys("21-Nov-2015")
currency = driver.find_element_by_xpath("//*[@id='currencyCode']")
options = currency.find_elements_by_tag_name("option")
optionsList = []
for option in options: #iterate over the options, place the attribute value in list
optionsList.append(option.get_attribute("value"))
for optionValue in optionsList:
print "starting loop on option %s" % optionValue
driver.execute_script("document.getElementById('currencyCode').style.display = 'block';")
driver.find_element_by_xpath("//*[@id='currencyCode']/option[@value='"+optionValue+"']").click()
driver.find_element_by_css_selector('input[type=\"submit"]').click()
time.sleep(5)
infile="ice."+optionValue+"."+x+".txt"
with open(infile,"w") as ice_content:
data=[]
for tr in driver.find_elements_by_xpath('//table[@class="table table-responsive table-data"]//tr'):
tds=tr.find_elements_by_tag_name('td')
if tds:
data=([td.text for td in tds])
ice_content.write(str("|".join(data))+"\n")
打印第一个文本文件后,它向我抛出错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
以下是html代码段:
<label for="currencyCode">Currency</label>
<select id="currencyCode" name="criteria.currencyCode" class="form-control" style="display: none;">
<option value="CHF">CHF</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="JPY">JPY</option>
<option value="USD">USD</option>
</select>
<div class="chosen-container chosen-container-single chosen-container-single-nosearch" style="width: 297px;" title="" id="currencyCode_chosen">
货币
瑞士法郎
欧元
英镑
日元
美元
一定要告诉我哪里出了问题,提前谢谢 您不需要通过xpath查找
currencyCode
对象,您可以像以前一样通过ID查找currency=driver。通过ID(“currencyCode”)查找元素
您能否至少给出一个发生断言的行号,因为有几个地方可能会引发异常。很抱歉,但我似乎无法从上面的URL找到解决方案。要找到解决方案,我必须在第二个for循环之前打开文本文件。这给了我答案
driver=webdriver.Chrome()
driver.get('url');
time.sleep(3)
ele=driver.find_element_by_id("reportDate")
ele.clear()
ele.send_keys("21-Nov-2015")
currency = driver.find_element_by_xpath("//*[@id='currencyCode']")
options = currency.find_elements_by_tag_name("option")
optionsList = []
for option in options: #iterate over the options, place the attribute value in list
optionsList.append(option.get_attribute("value"))
for optionValue in optionsList:
print "starting loop on option %s" % optionValue
infile="ice."+optionValue+"."+x+".txt"
with open(infile,"w") as ice_content:
driver.execute_script("document.getElementById('currencyCode').style.display = 'block';")
driver.find_element_by_xpath("//*[@id='currencyCode']/option[@value='"+optionValue+"']").click()
driver.find_element_by_css_selector('input[type=\"submit"]').click()
time.sleep(5)
data=[]
for tr in driver.find_elements_by_xpath('//table[@class="table table-responsive table-data"]//tr'):
tds=tr.find_elements_by_tag_name('td')
if tds:
data=([td.text for td in tds])
ice_content.write(str("|".join(data))+"\n")