无法使用selenium webdriver定位按钮元素

时间:2015-03-03 09:31:20

标签: python selenium selenium-webdriver

我一直在尝试找到嵌套的按钮元素(<button class="dwnload_xls")并单击它但未能这样做。有人可以帮我这个吗? 我已经尝试过使用以下内容(单独使用),但所有这些内容都只是给出相同的响应:

  

无法找到元素

我的代码:

frame = WebDriverWait(browser, 30).until(lambda x: x.find_element_by_xpath(".//*[contains(@class, 'downloadxlsform')]//div[contains(@class,'downloadform')]//button[contains(@class, 'dwnload_xls')]"))

HTML code:

<form action="/bdataservlet" class="downloadxlsform" method="GET" name="downloadxlsform">
<div class="downloadform" name="downloadform">
<div class="sub_title">All traded products</div>
<input id="methodxls" name="method" type="hidden" value="InstrumentList"><input name="profileid" type="hidden" value="1124"><button class="dwnload_xls" name="SUBMIT" onclick="this.form.submit()" style="background-image:url(/sites/bet/images/english/download_xls_eng_white.gif)" type="submit"></button>
</div>
</form>

1 个答案:

答案 0 :(得分:1)

您可以让selenium找到该按钮并通过submit()提交表单:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

form = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "form.downloadxlsform")))
form.submit()

此外,如果表单位于iframe内,您需要switch to it first

driver.switch_to.frame('frame_name')