Selenium如何点击

时间:2014-12-19 12:35:25

标签: python selenium

这是我的源代码:

  <a id="form_week:btn_before" title="Précedent" onclick="var cf = function(){disableLink('btn_before');};var oamSF = function(){return oamSubmitForm('form_week','form_week:btn_before');};return (cf()==false)? false : oamSF();" href="#">

如何点击该链接?

编辑:

我测试了你的三个答案,但是

 mydriver.find_element_by_xpath('//a[@title="Précedent"]').click()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException

求助:

这是我的错,我没有看到元素'a'在iframe中。

mydriver.switch_to_frame(mydriver.find_element_by_xpath("//iframe"));
mydriver.find_element_by_id('form_week:btn_before').click()

4 个答案:

答案 0 :(得分:1)

点击标题为'Précedent'的元素'a'

driver.find_element_by_xpath('//a[@title="Précedent"]').click()

答案 1 :(得分:0)

您可以使用click命令并传递元素ID

selenium.click("form_week:btn_before")

答案 2 :(得分:0)

这是你如何做到的:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_id('form_week:btn_before').click()

答案 3 :(得分:0)

导入WebDriverWaitexpected_conditions,这会告诉您的驱动程序在尝试查找按钮时等待最多60秒。然后尝试使用它的ID:

来获取它
WebDriverWait(
    webdriver,
    60
).until(
    expected_conditions.visibility_of_element_located(
        (By.ID, 'form_week:btn_before'))
).click()