-1

Using Python and Selenium, I have tried multiple workarounds (xpath, css selector, id, etc) to capture the button on this particular page and then attempting to click on it. Nothing seems to be sticking, any solutions? Seems as though it has something to do with how the 'link' itself is written on the site I am accessing. I am trying to have the program click the 'Matrix' link below the matrix image.

My code:

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

driver = webdriver.Chrome("C:\\Users\\Matt\\Documents\\WebDriver\\chromedriver_win32\\chromedriver.exe")
#This site requires log in entry so you wont be able to access it yourself.
driver.get("https://www.stellarmls.com/")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']"))).click()

print(driver.page_source)

The website's source code for 'Matrix' link:

<h4 _ngcontent-c113="" class="apptitle ng-star-inserted" style="">Matrix</h4>

Screenshots provided for better idea of what is going on. Any recommendations would be appreciated, I have combed stackoverflow and found similar issues but their problems were resolved with very simple alterations which I tried myself to no avail.

Traceback error:

Traceback (most recent call last):
  File "C:\Users\Matt\Documents\Splitt\ROI Property Finder.py", line 54, in <module>
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']"))).click()
  File "C:\Users\Matt\Python3.9\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Supplemental screenshots: enter image description here

Using an Selenium IDE it informs me that the xpath should be what I have in my current code, seen below:

enter image description here

enter image description here

Mattr42
  • 51
  • 6
  • your code tries to fnd xpath using @id , but the element h4 in your question doesn't have any id , the exceptions shows entirely different loctor which is text . Are you sure you added the right code, element and locators – PDHide Jan 07 '21 at 16:39
  • @PDHide yep pretty sure, see below 'answer' for further detail on the issue that I am running into. – Mattr42 Jan 08 '21 at 01:11

1 Answers1

1

To click on the element with text as Matrix you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("div#appDetailMatrix h4.apptitle.ng-star-inserted").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']").click()
    

As the element is a Angular element ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#appDetailMatrix h4.apptitle.ng-star-inserted"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']"))).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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • This answer is very useful however it did not solve the problem. In fact, I have attempted these before posting. It is important to note that yes these are Angular elements and thus require time delays. I will update my code in the post to reflect your input but it still does not work. Traceback timeout since it couldn't find the element. – Mattr42 Jan 07 '21 at 19:20
  • @Mattr42 Can you crosscheck once if the element is within an ` – DebanjanB Jan 07 '21 at 19:22
  • New screenshot posted at bottom of OP. There is an iframe but the element is not explicitly including within it. The bottom arrow is where the element is contained. – Mattr42 Jan 07 '21 at 19:35
  • @Mattr42 Agree, the element is out of ` – DebanjanB Jan 07 '21 at 19:50
  • Yeah, any other suggestions? – Mattr42 Jan 07 '21 at 22:04