1

Hi I am trying to click this link using selenium but it does not get clicked

This is the HTML from the website

<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>

This is what I am trying in my code

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

Could you please check the code and advise me what I am doing wrong? This is the full code.

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()

I expect the tag href link to be clicked and to move to the next page.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Joe
  • 35
  • 4

2 Answers2

1

To click() on the element with text as Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984 you need to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using 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()
    
  • Using 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()
    
  • Using 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()
    
  • Using 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()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Delete [@href] from your Xpath, you need a Xpath to the webElement not to the atribute of the webElement.

Ștefan Teslari
  • 411
  • 4
  • 6