get locator from a hover element - python

I want to get the locator of this element (5,126,
601) but seem cant get it normally.
I think it will have to hover the mouse to the element and try to get the xpath but still I cant hover my mouse into it because it an SVG element . Any one know a way to get the locator properly?
here is the link to the website: https://fundingsocieties.com/progress

Well, this element is updated only by hovering over the chart.
This is the unique XPath locator for this element:
"//*[name()='text']//*[name()='tspan' and(contains(#style,'bold'))]"
The entire Selenium command can be:
total_text = driver.find_element(By.XPATH, "//*[name()='text']//*[name()='tspan' and(contains(#style,'bold'))]").text
This can also be done with this CSS Selector: text tspan[style*='bold'], so the Selenium command could be
total_text = driver.find_element(By.CSS_SELECTOR, "text tspan[style*='bold']").text
Well, CSS Selector looks much shorter :)

Clicking on each node in turn will lead to the accompanying text being placed in the highcharts-label element. This text can then be retrieved and the Quarter (1st tspan) be linked to the Total value (4th tspan) that you desire.
url="https://fundingsocieties.com/progress"
driver.get(url)
chart = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[#data-highcharts-chart='0']"))
)
markers = chart.find_elements(By.XPATH, "//*[local-name()='g'][contains(#class,'highcharts-markers')]/*[local-name()='path']")
for m in markers:
m.click()
try:
element = WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.XPATH, "//*[local-name()='g'][contains(#class,'highcharts-label')]/*[local-name()='text']"))
)
tspans = element.find_elements(By.XPATH, "./*[local-name()='tspan']")
if len(tspans) > 3:
print ("%s = %s" % (tspans[0].text, tspans[3].text))
except TimeoutException:
pass
The output is as follows:
Q2-2015 = 2
Q3-2015 = 12
....
Q1-2022 = 5,076,978
Q2-2022 = 5,109,680
Q3-2022 = 5,122,480
Q4-2022 = 5,126,601

Related

How to click on element that contains text from list?

I'm trying to click on element by xpath that contains text from list.
categories = (WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.sph-MarketGroupNavBarButton '))))
categorylist = []
for category in categories:
text = category.text
categorylist.append(text)
print(text)
categoryindex = categories[3]
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()
print(categoryindex)
When I run it, the script creates the list and prints out list content and then I want to search for element that contains the text from the list and click on it, but this part of code doesn't work : It doesn't click or print the specified text. Nothing happens and I get timeout error.
categoryindex = categories[3]
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()
print(categoryindex)
When I replace {categoryindex} with actual text from list it clicks on it.
Change
categoryindex = categories[3]
to
categoryindex = categorylist[3]

How to right-click or move mouse to a element used selenium with python?

For Example: Get icon of google
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME,'body'))
)
gmail = driver.find_element(By.LINK_TEXT, 'Gmail')
google_icon = driver.find_element(By.CLASS_NAME, "lnXdpd")
action = ActionChains(driver)
action.context_click(google_icon)
finally:
pass
the context_click don't work.so I try
# test second
action.move_to_element(google_icon)
# action.context_click(on_element=None)
action.context_click(google_icon)
It's don't work too. but
gmail.click()
this is can work.what should i do.

Scroll on a specific DIV Element to the bottom in Python Selenium

I'm trying to do a simple Python Selenium automation on a website while the website is blocked by a dialog which needs to scroll down to see all the paragraph so as to pass into the website.
I tried to use the code below to scroll the paragraph, but unsuccessful.
driver = webdriver.Chrome('chromedriver')
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.fidelity.com.hk/en/our-funds/mpf')
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[data-action="button68"]'))).click()
time.sleep(1)
ele =driver.find_element_by_css_selector('.content-scrolling-behavior')
driver.execute_script("return arguments[0].scrollIntoView(true);", ele)
html capture
I would appreciate any feedback on how to consistently select an option from the dropdown noted in the code provided. And here is the website I looking at: https://www.fidelity.com.hk/en/our-funds/mpf
You can scroll using ActionChain like this :
also, in that div, there are 27 li tags, so I am doing xpath indexing and then one by one I am moving driver focus to those li.
Sample code :
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.fidelity.com.hk/en/our-funds/mpf")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[data-action="button68"]'))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container")))
list_size = len(wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[#class='list']/li"))))
print(list_size)
j = 1
for i in range(list_size):
ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, f"(//ul[#class='list']/li)[{j}]")))).perform()
j = j + 1
time.sleep(1)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class$='btn-confirm']"))).click()
This should work
ele =driver.find_element_by_css_selector('div.content-scrolling-behavior')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", ele)
UPD
Try this instead:
ele =driver.find_element_by_css_selector('div.container')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", ele)

Clicking on the correct option from a given list of options in Selenium (Python)

I am trying to give input to the 'Currency I have' text box. This input is in the variable 'currency'. After I give the input in the text box, it displays a number of options. I want to do a case insensitive match on the 3 letter currency code I enter and the options the text box displays in the drop-down, and select the correct one.
The page I am testing: https://www.oanda.com/fx-for-business/historical-rates
currency = currency_list.loc[i,'currency']
print(f'\nFETCHING DATA FOR : {currency}')
df=pd.DataFrame()
#input our currency in "currency I have" text-box
WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#havePicker > div'))).click()
currency_have = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#havePicker > div > input')))
try:
currency_have.clear()
except:
pass
currency_have.send_keys(currency)
options = WebDriverWait(driver, 2).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'#havePicker > div > ul li')))
div_tags = [li_tags.find_elements_by_tag_name('div') for li_tags in options]
for div_tag in div_tags:
test = div_tag[1]
if (test.text.casefold()) == (currency.casefold()):
return test
else:
continue
the return statement in my code is incorrect. How do I proceed further to achieve my goal?
Please, any help would be appreciated. I'm new to selenium.
In order to wait for dropdown result, the order of your actions should be the following:
Clear "Currency I have" dropdown
Click it (not completely sure if this action is required, test it)
Wait for all values
Send value ("Australian dollar") # not AUD as there is also Saudi Riyal in search results
This part of code should look like below:
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#havePicker > div'))).click()
currency_have = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#havePicker > div > input')))
currency_have.clear()
currency_have.click()
options = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#havePicker > div > ul li')))
currency_have.send_keys("Australian dollar")
Please note that you have two results on AUD. That's why I used the full currency name. If you want to use just aud, use split() Check here how to use it How to extract the substring between two markers?
Then improve your for loop.

How to find a row and click a table element across the table with selenium?

I want to click the link tied to the label for this td.
I can use the onclick to find one item link,but the name changes from HemoGlobin A1C, to HGB A1c, etc and the onclick has no unique ID to search for everytime.
using this now:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[contains(#onclick, '%s' )]" % testname))).click()
testname = 'A1c'
Please try this:
testname= "a1c"
try:
elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//td[contains(translate(text(), "AC", "ac"), {})]/following-sibling::td[3]//td'.format(testname))))
except TimeoutException:
print("Element not found")
else:
elem.click()
Explanation:
//td[contains(translate(text(), "AC", "ac"), testname)]: First find a td element which contains text 'A1C' or 'A1c' (or 'a1C' or 'a1c'). Here transalte() is a xpath function which will replace all 'A' & 'C' with 'a' & 'c'.
/following-sibling::td[3]//td Then we have to go to a sibling of that td element, which in your case is a third sibling of same type, and then we find the child element td in it.
Please try this one to check whether works.
testname = "A1c"
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[text()[contains(.,'" + testname + "')]]")))
element.click

Categories

Resources