2

I want to download user data on Google analytics by using crawler so I write some code using selenium. However, I cannot click the "export" button. It always shows the error "no such element". I tried to use find_element_by_xpath, by_name and by_id. I upload inspect of GA page below.

I TRIED:

 driver.find_element_by_xpath("//*[@class='download-link']").click()
    driver.find_element_by_xpath('//*[@id="ID-activity-userActivityTable"]/div/div[2]/span[6]/button')
    driver.find_element_by_xpath('//*[@class='_GAD.W_DECORATE_ELEMENT.C_USER_ACTIVITY_TABLE_CONTROL_ITEM_DOWNLOAD']')

Python Code:

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


driver = webdriver.Chrome('/Users/parkjunhong/Downloads/chromedriver')
driver.implicitly_wait(3)
usrid = '1021'
url = 'https://analytics.google.com/analytics/web/#/report/app-visitors-user-activity/a113876882w169675624p197020837/_u.date00=20190703&_u.date01=20190906&_r.userId='+usrid+'&_r.userListReportStates=%3F_u.date00=20190703%2526_u.date01=20190906%2526explorer- 
table.plotKeys=%5B%5D%2526explorer-table.rowStart=0%2526explorer- 
table.rowCount=1000&_r.userListReportId=app-visitors-user-id'
driver.get(url)
driver.find_element_by_name('identifier').send_keys('ID')
idlogin = driver.find_element_by_xpath('//*[@id="identifierNext"]/span/span')
idlogin.click()

driver.find_element_by_name('password').send_keys('PASSWD')
element = driver.find_element_by_id('passwordNext')
driver.execute_script("arguments[0].click();", element)
#login
driver.find_element_by_xpath("//*[@class='download-link']").click()
#click the download button

ERROR:

Message: no such element: Unable to locate element

inspection of GA

3
  • 1
    Hi Can you Please Share Your website Link? Commented Sep 14, 2019 at 10:52
  • Or Can You Please Give Me Full inspect picture so i can check Commented Sep 14, 2019 at 10:53
  • 1
    @HamzaLachi I really appreciate your attention. I upload more inspect picture. Commented Sep 14, 2019 at 12:13

2 Answers 2

1

your click element is in an iFrame (iFrame id="galaxyIframe" ...). Therefore, you need to tell the driver to switch from the "main" page to said iFrame. If you add this line of code after your #login it should work: driver.switch_to.frame(galaxyIframe)

(If the frame did not have a name, you would use: iframe = driver.find_element_by_xpath("xpath-to-frame") and then driver.switch_to.frame(iframe)

To get back to your default frame, use: driver.switch_to.default_content()

Crawling GA is generally a pain. Not just because you have these iFrames everywhere. Apart from that, I would recommend looking into puppeteer, the new kid on the crawler block. Even though the prospect of switching to javascript from python may be daunting, it is worth it! Once you get into it, selenium will have felt super clunky.

Sign up to request clarification or add additional context in comments.

1 Comment

Im really appreciate for your help. It is work! I really spend a lot of time into this problem. Again, really thanks for your help!
0

You can try with the text:

If you want to click on 'Export'-

//button[contains(text(),'Export')]

2 Comments

I already tried but it still show same error... thanks for reply
yeah sure, please go ahead, the time i replied the dom was partial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.