【问题标题】:How can I click on this link with selenium using python?如何使用 python 使用 selenium 单击此链接?
【发布时间】:2018-07-05 15:50:35
【问题描述】:

我只是编程的初学者,我正在尝试学习如何自动化 Web 程序。 我的工作基于https://www.coeweb.istat.it/

我需要找到页面的元素,我已经尝试过了 与driver.find_element_by_xpath 但由于我对 html 代码不太熟悉,我什至不确定是否使用正确的代码作为该方法的参数。 有人可以将代码示例发送给我,以单击页面中间的粗体链接qui.吗? 这是我尝试过的:

from selenium import webdriver
from selenium.webdriver.common.by import By
url = "https://www.coeweb.istat.it/"
driver = webdriver.Chrome()
driver.get(url)
link = driver.find_element_by_xpath('//[@id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong')
link.click()

不过,我得到的错误是:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong"}

【问题讨论】:

  • 试试这个css选择器a[href="lnkdati.htm"]或者跳过完全点击按钮,直接导航到driver.get("https://www.coeweb.istat.it/lnkdati.htm")的链接

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

根据URL,您已将链接与文本共享为qui. 带有<frame>。因此,您必须切换到所需的诱导 WebDriverWait 的帧,然后再次诱导 WebDriverWait,同时注意以下元素:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    url = "https://www.coeweb.istat.it/"
    driver.get(url)
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"principale")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[@id='AutoNumber1']//tr//td//p//a[@href='lnkdati.htm' and .//strong[contains(.,'qui.')]]"))).click()
    
  • 浏览器快照:

【讨论】:

    【解决方案2】:

    你必须等到它可以点击:

    # I have fixed xpath
    element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@id='AutoNumber1']//a[contains(., 'qui.')]"))
        )
    element.click()
    

    注意:你必须做一些导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

    也可以使用 CSS Selector 来定位元素:

    #AutoNumber1 > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > font > a
    

    element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#AutoNumber1 > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > font > a"))
        )
    element.click()
    

    编辑:您必须先切换到框架,然后才能像这样定位您的元素:

    driver.switch_to.frame(driver.find_element_by_xpath("//frame[@name = 'principale']"))
    

    所以完整的代码会是这样的:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    url = "https://www.coeweb.istat.it/"
    driver = webdriver.Chrome()
    driver.get(url)
    
    driver.switch_to.frame(driver.find_element_by_xpath("//frame[@name = 'principale']")) # switches to frame
    element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@id='AutoNumber1']//a[contains(., 'qui.')]"))
        )
    element.click()
    driver.switch_to.default_content() # switch back to default content
    

    解释:您不能与iframeframe 中的元素进行交互。为了能够做到这一点,你必须找到一个frame,切换到它,做一些事情,然后切换回默认内容

    【讨论】:

    • 谢谢我试过了,但这就是我现在得到的“selenium.common.exceptions.TimeoutException: Message:”。这是我输入的内容: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC url = "coeweb.istat.it" driver = webdriver .Chrome() driver.get(url) element = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.XPATH, '//[@id="AutoNumber1"]/tbody/tr[3]/td [2]/p[3]/font/a/strong')) ) element.click()
    • 我已经修复了xPath,请再试一次
    • 我又试了一次,但仍然收到“selenium.common.exceptions.TimeoutException: Message:”。我真的不知道为什么。
    • 我已经找到问题并添加了工作代码,请看一下。如果您对我的回答感到满意,请点击投票按钮下方的灰色复选按钮进行标记。
    • 对不起,我是新手,对网站不太实用。我一定点击了两次
    猜你喜欢
    • 2020-09-23
    • 2021-12-03
    • 1970-01-01
    • 2017-12-25
    • 2020-04-13
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多