Reputation: 164
I have following problem. I am trying to script downloading file, which is being daily uploaded on a website. file name is "My Report $date.xlsx" (so Today it's "My Report 20190917.xlsx).
The only value that is constant is title of it, actually part of it without the date. hastooltip and href are changing.
Here is the code I need to use:
<a class="entry-title" rel="bookmark" title="My Report 20190917.xlsx" href="https://xxxx/foo/bar/something.html" hastooltip="dijit_Tooltip_10400"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>My Report 20190917.xlsx</a>
And here is what I have googled/ tried so far, I think this is very near:
selenium find element by title that contains?
I tried using same thing with xpath instead of css_selector, but no luck (title^= instead of title=: )
driver.find_element_by_xpath("//div[@id='list']/div[@role='region']/table[@role='presentation']/tbody/tr[15]//a[title^='My Report']").click()
I also tried using some options like "title contains" and tried to use variable instead of 'My Report' name like:
import time
my_file =("My Report " + time.strftime("%Y%m%d") + ".xlsx")
and putting my_file variable into the xpath search, but that did not work at all.
Shortly I am out of ideas and kindly asking for help.
Upvotes: 0
Views: 230
Reputation: 193088
The desired element is a dynamic 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 solutions:
Using XPATH
:
from datetime import datetime
date_time_now = datetime.now()
my_string = "My Report "+date_time_now.strftime("%Y%m%d")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='entry-title' and starts-with(@title, '" + my_string + "')]"))).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
Upvotes: 0
Reputation: 708
I believe it's a possible solution:
from selenium.webdriver.common.by import By
from datetime import datetime
today = datetime.now()
today = today.strftime("%Y%m%d")
exp = "//*[@title='My Report "+today+".xlsx']"
myfile = driver.find_element(By.XPATH,(" %s" % exp))
myfile.click()
I hope it helps
I believe you are working with a content not availble online. So, I cannot try it. The expression I gave you return this:
Element='<a class="entry-title"
hastooltip="dijit_Tooltip_10400"
href="https://xxxx/foo/bar/something.html"
rel="bookmark"
title="My Report 20190917.xlsx"/>'
But what you actually need is the path to the xls. If the link for the xls is the href above, you can do this:
myfile.get_attribute('href')
I don't know if it's enough to do the download or if you have to open by command:
driver.get(myfile.get_attribute('href'))
Upvotes: 1