2

Pic of the website's inspect element More in Depth pic My Code snippet

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import requests
///
excel = driver.find_element_by_name('Excel')
excel.click()

I then get this when I try to run it, any help would be appreciated

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="Excel"]"}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
NotGood911
  • 57
  • 6
  • Can you share the url? As Excel might be nested within some elements it might not be accessed directly. You can refer this link https://stackoverflow.com/questions/50354465/python-selenium-unable-to-click-button – Karthik Aug 16 '20 at 05:57
  • I'm automating some work processes and this URL pertains to my work so I'd rather not share, i'm sure you understand. Is there any other type of information I could share that would help? – NotGood911 Aug 16 '20 at 06:32

2 Answers2

2

The desired element is a Angular element, so to click on the element you have 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, "button[mat-button] > span.mat-button-wrapper span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mat-button]/span[@class='mat-button-wrapper']//span[text()='Excel']"))).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
  • 1
    Thank you for the input, I did manage to find a different solution to my problem, but I did run your solution and it also worked! – NotGood911 Aug 16 '20 at 23:02
  • 1
    I will have to read up on angular elements, as I did not know how to manage them, also made sure to upvote you – NotGood911 Aug 16 '20 at 23:04
1

driver.find_element_by_name('Excel') selects a tag with name='Excel'.

For example, it would find a tag like this:

<div name='Excel'>Hello</div>.

The CSS equivalent for find_element_by_name is [name="Excel"].

From the pic of your website's inspect element, it seems that you are trying to find an element with the text 'Excel' inside the div, so instead, you need to use the following function:

driver.find_element_by_link_text('Excel')

Hope that helped!

For more on Python selenium webdriver, use this link. It helped out a lot for me!

hams222
  • 19
  • 3
  • hello I just tried this method and got this error NoSuchElementException Traceback (most recent call last) in 43 apply.click() 44 sleep(3) ---> 45 excel = driver.find_element_by_link_text('Excel') 46 excel.click() – NotGood911 Aug 16 '20 at 06:47
  • Ah I see, I'm sorry I didn't realize you're clicking on a span. Clicking on link_text might only work on tags. I would recommend you try out driver.find_element_by_css_selector('.mat-button-wrapper span') then! – hams222 Aug 16 '20 at 06:55
  • Once do I print that to see the element?I had it set to click and it gave me the same error, sorry if im asking alot of questions, still brand new. – NotGood911 Aug 16 '20 at 07:08
  • Hmmm... Seems like span isn't a clickable element actually. Do you absolutely need to click on it? If you really do, I recommend you look into how to do it in JavaScript because driver can execute JavaScript code using driver.execute_script(). However, if you simply do driver.find_element_by_css_selector("button.mat-focus-indicator") I think it'll work fine too. Try the latter first. – hams222 Aug 16 '20 at 07:30
  • When I ran that on the actual website it ended up clicking something else, probably shares that tag right? This button is used to download a report. Clicking on it initializes the download. – NotGood911 Aug 16 '20 at 07:36
  • Alright fam I gotchu. driver.find_element_by_css_selector("button.mat-focus-indicator:nth-child(3)"). This selects the button that is the third child of its parent, which is the Excel button if I'm not wrong. – hams222 Aug 16 '20 at 07:42
  • aw damn I thought that would solve it but it ended up clicking the same thing as the last one – NotGood911 Aug 16 '20 at 07:51
  • I also altered the last (3) and tried 1 and 2, and it kept clicking the same drop down arrow. – NotGood911 Aug 16 '20 at 07:57
  • I don't think I know enough to help you unfortunately. I recommend you to go get the extension from your browser's web store Selenium IDE. Play around with it and it should help you find a way to select that button. I also realized that I should've mentioned that at the beginning, sorry I'm a new user so I'm a bit clumsy at helping. – hams222 Aug 16 '20 at 08:20
  • ok when I click the button with the IDE recording it gives me the xpath //button[3]/span/span so then I did excel = driver.find_element_by_xpath("//button[3]/span/span") link.click() this clicked on the same drop down as the line you sent me previously – NotGood911 Aug 16 '20 at 21:25
  • I also can do xpath=//button[contains(.,'Excel')] But when I do driver.find_element_by_xpath it still clicks on the same drop down... – NotGood911 Aug 16 '20 at 21:36
  • Damn, I really don't understand why even that doesn't work. Can you send me the code possibly through Google Drive or Github so that I can inspect it further? – hams222 Aug 16 '20 at 21:58
  • Ah, ok ill upload to gethub later, but is the way I did the driver.find_element_by_xpath correct? – NotGood911 Aug 16 '20 at 22:12
  • 1
    ok so I managed to find a work around excel = driver.find_element_by_xpath("//button[3]") actions = webdriver.common.action_chains.ActionChains(driver) actions.move_to_element_with_offset(excel, 20, 20) actions.click() actions.perform() – NotGood911 Aug 16 '20 at 22:57
  • oh nice that's smart you just move it to where you want it XD – hams222 Aug 17 '20 at 03:31