编辑:在此page上,我正在尝试提交参数,以便从数据库生成报告。我已经能够使用第一个字段的新开始日期发送send_keys()。第二个字段(结束日期)保持不变。对于第三个字段,除第一个字段外,我需要单击(Select All)
。
我需要单击(Select All)
,只有在单击父'divDropDown'之后,该列表才会显示在隐藏的下拉列表中。激活后,此隐藏表将显示一个tbody,其中5-6 tr / td / span设置包含各种选项。您可以在ID ctl31_ctl04_ctl07_divDropDown_ctl00
下找到全选选项。
在HTML的更深处,有一个dropDownButton,它不会在代码中产生错误(前两行),但也不会像手动操作那样在浏览器中显示为激活该下拉菜单,所以我认为这无济于事在这里。
我已经搜索了相关的答案,并尝试使用Selenium中的各种代码(wait,execute_script等)来访问此项目,甚至尝试切换到其上方的iframe(在单击输入时似乎已激活)即使所寻找的元素不在其附近),但仍会收到错误消息“无法找到元素...”。 这是尝试过的各种代码:
driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07"]')#.click()
driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_ddDropDownButton"]').click()
#driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_divDropDown"]').click()
#driver.execute_script("event.cancelBubble=true;")
#driver.execute_script("arguments[0].click();", elem)
#driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_divDropDown_HiddenIndices" and @type="hidden"]')
#elem1 = driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_divDropDown_ctl00" and @type="checkbox"]')#.click()
elem1 = driver.find_element_by_xpath('//*div[@id="ctl31_ctl04_ctl07_divDropDown"]/.//span[@id="ctl31_ctl04_ctl07_divDropDown_ctl00"]').get_attribute("innerHTML")
driver.execute_script("arguments[0].click();", elem1)
selector = '#ctl31_ctl04_ctl07_divDropDown_ctl00'
selector = '#ctl31_ctl04_ctl07_divDropDown > span > div:nth-child(1) > table > tbody > tr:nth-child(1) > td > span > label'
path = '//*[@id="ctl31_ctl04_ctl07_divDropDown"]/span/div[1]/table/tbody/tr[1]/td/span/input'#label'
#path = '//*[@id="ctl31_ctl04_ctl07_divDropDown_ctl00"]'
#driver.find_element_by_css_selector(selector).click()
driver.find_element_by_xpath(path).click()
我认为它无法定位,因为它通常是隐藏的,但是无论我如何尝试通过按钮或单击div单击或执行下拉菜单,它都不会保持打开状态(我确实看到它向下闪烁有时,它只是重新关闭,因此表及其下拉菜单再次被隐藏)。我还尝试了以下代码对其进行测试。使用常规的wait命令,经过一些迭代后,我会收到“ stale element”错误。没有任何等待,我将找不到元素。通过“等到”,我可以看到下拉列表在上/下移动,但是似乎看不到“全选”吗?所有这些操作就是打开/关闭带有我的打印语句的下拉列表,以查看何时(在打印“检查是否存在”之后)。我也尝试直接执行“全选脚本"$get('ctl31_ctl04_ctl07').control.OnSelectAllClick(this);"
”,但无济于事。
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
for i in range(15):
#driver.implicitly_wait(1000)
#driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_ddDropDownButton"]').click()
wait(driver, 1000).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ctl31_ctl04_ctl07_ddDropDownButton"]'))).click()
#elem = driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_divDropDown_ctl00"]')
print('checking if exists')
if check_exists_by_xpath('//*[@id="ctl31_ctl04_ctl07_divDropDown_ctl00"]'):
driver.find_element_by_xpath('//*[@id="ctl31_ctl04_ctl07_divDropDown_ctl00"]').click()
print('did it!')
else: print('past dropdown button %d' % i)
i +=1
这是HTML(这是一个登录网站,因此我无法显示其他内容,对不起,图片:html
<iframe id="ctl31_ctl04_ctl07_ctl01" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="display:none;position:absolute;z-index:10;" src="javascript:'';" frameBorder="0" title="Borough: place holder" longdesc="Borough: place holder" Name="ctl31_ctl04_ctl07_ctl01"></iframe>
<div id="ctl31_ctl04_ctl07_divDropDown" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="display:inline-block;border-color:DarkGray;border-width:1px;border-style:Solid;overflow:auto;background-color:window;display:none;position:absolute;z-index:11;">
<span style="margin: 0px; background-color: window;"><div style="overflow: auto; width: 186px; height: 133px;">
<table cellpadding="0" cellspacing="0" style="background-color:window;">
<tbody>
<tr>
<td nowrap="nowrap">
<span>
<input id="ctl31_ctl04_ctl07_divDropDown_ctl00" type="checkbox" name="ctl31$ctl04$ctl07$divDropDown$ctl00" onclick="$get('ctl31_ctl04_ctl07').control.OnSelectAllClick(this);"><label for="ctl31_ctl04_ctl07_divDropDown_ctl00">(Select All)</label>
</span></td></tr>
<tr>
<td nowrap="nowrap"><span><input id="ctl31_ctl04_ctl07_divDropDown_ctl01" type="checkbox" name="ctl31$ctl04$ctl07$divDropDown$ctl01" onclick="$get('ctl31_ctl04_ctl07').control.OnValidValueClick(this, 'ctl31_ctl04_ctl07_divDropDown_ctl00');"><label for="ctl31_ctl04_ctl07_divDropDown_ctl01">MANHATTAN </label></span></td></tr>
<tr>
<td nowrap="nowrap"><span><input id="ctl31_ctl04_ctl07_divDropDown_ctl02" type="checkbox" name="ctl31$ctl04$ctl07$divDropDown$ctl02" onclick="$get('ctl31_ctl04_ctl07').control.OnValidValueClick(this, 'ctl31_ctl04_ctl07_divDropDown_ctl00');"><label for="ctl31_ctl04_ctl07_divDropDown_ctl02">BROOKLYN </label></span></td></tr>
<tr>
<td nowrap="nowrap"><span><input id="ctl31_ctl04_ctl07_divDropDown_ctl03" type="checkbox" name="ctl31$ctl04$ctl07$divDropDown$ctl03" onclick="$get('ctl31_ctl04_ctl07').control.OnValidValueClick(this, 'ctl31_ctl04_ctl07_divDropDown_ctl00');"><label for="ctl31_ctl04_ctl07_divDropDown_ctl03">QUEENS </label></span></td></tr>
<tr>
<td nowrap="nowrap"><span><input id="ctl31_ctl04_ctl07_divDropDown_ctl04" type="checkbox" name="ctl31$ctl04$ctl07$divDropDown$ctl04" onclick="$get('ctl31_ctl04_ctl07').control.OnValidValueClick(this, 'ctl31_ctl04_ctl07_divDropDown_ctl00');"><label for="ctl31_ctl04_ctl07_divDropDown_ctl04">BRONX </label></span></td></tr>
<tr>
<td nowrap="nowrap"><span><input id="ctl31_ctl04_ctl07_divDropDown_ctl05" type="checkbox" name="ctl31$ctl04$ctl07$divDropDown$ctl05" onclick="$get('ctl31_ctl04_ctl07').control.OnValidValueClick(this, 'ctl31_ctl04_ctl07_divDropDown_ctl00');"><label for="ctl31_ctl04_ctl07_divDropDown_ctl05">STATEN ISLAND </label></span></td></tr>
</tbody>
</table>
<input type="hidden" name="ctl31$ctl04$ctl07$divDropDown$HiddenIndices" id="ctl31_ctl04_ctl07_divDropDown_HiddenIndices" value="">
</div>
<div style="height: 16px; width: 100%; margin: 0px; border-top: 1px solid lightgray; background-color: window; direction: ltr;"></div>
</span>
</div>
网页的外观如下:page
有什么想法吗?
答案 0 :(得分:1)
要点击下拉菜单选项,将其文本设为(全选),因为元素是JavaScript启用的元素,您需要为所需的 WebDriverWait 引入 em>可以点击的元素,您可以使用以下解决方案:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@id, '_divDropDown') and contains(@onclick, 'cancelBubble')][contains(@onactivate, 'cancelBubble')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@id, '_divDropDown') and contains(@onclick, 'cancelBubble')][contains(@onactivate, 'cancelBubble')]/span/div/table/tbody/tr/td/span//label[contains(@for, '_divDropDown_') and contains(., '(Select All)')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC