【问题标题】:How to click on the link using Selenium and Python如何使用 Selenium 和 Python 点击​​链接
【发布时间】:2019-04-10 06:22:01
【问题描述】:

您好,我正在尝试使用 selenium 单击此链接,但没有被单击

这是来自网站的 HTML

<td class=" sorting_1" data-key="title" data-label="Title"><a class="document-link" href="/view/html/inforce/current/act-1984-051?query=((Repealed%3DN+AND+PrintType%3D%22act.reprint%22+AND+PitValid%3D%40pointInTime(20190411000000))+OR+(Repealed%3DN+AND+PrintType%3D%22reprint%22+AND+PitValid%3D%40pointInTime(20190411000000)))+AND+Title%3D(%22Aboriginal+and+Torres+Strait+Islander+Communities+(Justice%2C+Land+and+Other+Matters)+Act+1984%22)&amp;dQuery=Document+Types%3D%22%3Cspan+class%3D'dq-highlight'%3EActs%3C%2Fspan%3E%2C+%3Cspan+class%3D'dq-highlight'%3ESL%3C%2Fspan%3E%22%2C+Search+In%3D%22%3Cspan+class%3D'dq-highlight'%3ETitle%3C%2Fspan%3E%22%2C+Exact+Phrase%3D%22%3Cspan+class%3D'dq-highlight'%3EAboriginal+and+Torres+Strait+Islander+Communities+(Justice%2C+Land+and+Other+Matters)+Act+1984%3C%2Fspan%3E%22%2C+Point+In+Time%3D%22%3Cspan+class%3D'dq-highlight'%3E11%2F04%2F2019%3C%2Fspan%3E%22">Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984</a></td>

这就是我在我的代码中尝试的内容

act_link = browser.find_element_by_xpath("//a[@class='document-link'][@href]").click()

能否请您检查代码并告诉我我做错了什么? 这是完整的代码。

import time
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
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import csv
options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : r'C:\Users\u0119342\Desktop\LEG_DOWNLOAD\Legislation@Koru\WORD\missing'}
options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(executable_path=r'D:\CHROME\chromedriver.exe', chrome_options=options)
browser.get('https://www.legislation.qld.gov.au/')
for parameter in ['Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984']:
    search_button = browser.find_element_by_xpath("//li/a[@href='/search/inforce']")
    search_button.click()
    search_field = browser.find_element_by_xpath("//div[@class='col-sm-8']/input[@type='text'][@class='form-control ui-autocomplete-input']")
    search_field.send_keys(parameter)
    search_in = browser.find_element_by_xpath("//select[@name='searchin']/option[@value='Title']")
    search_in.click()
    search_using = browser.find_element_by_xpath("//select[@name='searchusing']/option[@value='exactwords']")
    search_using.click()
    search_button1 = browser.find_element_by_xpath("//div [@class='col-sm-offset-2 col-sm-10']/button[@class='btn btn-primary btn-form']")
    search_button1.click()
    try:
        act_link = browser.find_element_by_xpath("//a[@class='document-link'][@href]").click()
    except NoSuchElementException:
        try:
            leg_his = browser.find_element_by_xpath("//a[@class='btn btn-default']/span[@id='glyphicon glyphicon-calendar']/span[@id='view-lh']")
            leg_his.click()
        except NoSuchElementException:
            try:    
                act_text = browser.find_element_by_xpath("//div[@id='lhview']/div[@class='content']")
                with open('output.rtf', 'w') as f:
                    f.write(act_text.text)
            except NoSuchElementException:
                time.sleep(10)
browser.back()
time.sleep(10)
browser.quit()

我希望标签 href 链接被点击并移动到下一页。

【问题讨论】:

    标签: python-3.x selenium-webdriver xpath css-selectors webdriverwait


    【解决方案1】:

    click() 元素上的文本为 Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984 您需要诱导 WebDriverWait 元素可点击,您可以使用以下任一Locator Strategies

    • 使用LINK_TEXT

      WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984"))).click()
      
    • 使用PARTIAL_LINK_TEXT

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984"))).click()
      
    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.sorting_1[data-key='title'][data-label='Title']>a.document-link[href^='/view/html/inforce/current/act-1984-051']"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class=' sorting_1' and @data-key='title'][@data-label='Title']/a[@class='document-link' and starts-with(@href, '/view/html/inforce/current/act-1984-051')]"))).click()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

      【解决方案2】:

      从您的 Xpath 中删除 [@href],您需要一个指向 webElement 而不是 webElement 属性的 Xpath。

      【讨论】:

      • 嗨斯特凡,尝试了你的建议,但仍然没有点击链接
      猜你喜欢
      • 2022-08-11
      • 2016-09-26
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多