Working with Selenium, clicking on JS as href link - python

I was hoping for maybe some clarification or suggestions on how to get selenium working with hyperlink. I have tried selecting it by pretty much every element possible. I have also attached a image with the source to look at and compare...
Example of html:
<div class="x-grid3-cell-inner x-grid3-col-ACTION_COLUMN" id="5005a00001rJ22q_ACTION_COLUMN"><span>Edit</span> | </div>
Examples:
#clicky = driver.find_element_by_xpath('//a[contains(#href,"javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);")]').click()
#clicky = driver.find_elements_by_partial_link_text('javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);').click()
#clicky = driver.find_element_by_xpath("//a[#href='javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);']").click()

Try this:
driver.find_element_by_xpath("//div[.//a[contains(#class,'chatterFollowUnfollowAction')]]//a[contains(#href,'javascript:srcUp')]").click()
In order to add an explicit wait before accessing this element use this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[.//a[contains(#class,'chatterFollowUnfollowAction')]]//a[contains(#href,'javascript:srcUp')]"))).click()

Try this:
someElement = driver.find_element_by_xpath("//a[contains(#class, 'chatterFollowUnfollowAction' and #title,'Follow this case')]")
driver.execute_script("arguments[0].click();",someElement)

Related

How to select a data-toggle in Selenium?

I would like to click on one of these tabs but I am not able to access it.
How can I access the data-toggle with Selenium in python?
Please try something like this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class=='section-tabs']//a[contains(#href,'timeseries-tab')]"))).click()
The code above should be able to select the timeseries tab.
You did not share the xml of the other tab, but I guess this should work:
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class=='section-tabs']//a[contains(#href,'heatplot-tab')]"))).click()
This can be also done with CSS Selectors.
To give better answer we need the link to that page or at least the entire XML of that page as a text, not picture

How to click on image using selenium

I'm trying to click on button that contains this code :
<div style="text-align:center" id="plus_res">
<img src="https://www.tunlancer.com/theme/images/plus_resv2.png" align="plus d'elements" title="plus d'elements" style="cursor:pointer" onclick="plus_resultat()">
</div>
i've tried :
image = driver.find_element_by_xpath("//img[contains(#src,'https://www.tunlancer.com/theme/images/plus_resv2.png')]")
driver.execute_script("arguments[0].click();", image)
driver.find_element_by_id('plus_res').click()
driver.find_element_by_xpath('//*[#id="plus_res"]/img').click()
But it's showing the NoSuchElementException .
How can i fix it please?
Possibly you have to add delay.
Something like this should work :
time.sleep(5)
driver.find_element_by_css_selector('img[src="https://www.tunlancer.com/theme/images/plus_resv2.png"]').click()
I guess you are missing a delay / wait before accessing that element.
Please try this:
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//img[#onclick="plus_resultat()"]'))).click()
You will need the following imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Also possibly the element is inside an iframe.

Click Selenium href link

I have a Selenium element with href="/daVinci/sys/systems/SK419114/dvmt
I am trying to click on it and go to the next page, my line of code is:
driver.find_element_by_link_texrt(" ").click()
So, what should be in the link text in the double quote?
Find element by link text wont work in this case as you need to know the text for that element like the below example :
Continue
where the following code will work but in your case you do not have such a text based link:
driver.find_element_by_link_texrt("Continue").click()
For your case can use this:
driver.find_element_by_xpath('//a[#href="/daVinci/sys/systems/SK419114/dvmt"').click();
See if this works:-
driver.find_element_by_xpath("//a[contains(#href,'daVinci/sys/systems')]").click()
You may want to try the same with explicit wait :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, '/daVinci/sys/systems')]"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Select element using XPATH with Python?

I am trying to determine the number of pages of data generated by the Indian Central Pollution Controal Board. Here is an example of output.
Following https://github.com/RachitKamdar/Python-Scraper, I used selenium/python
maxpage = int(browser.find_elements(By.XPATH,"//*[#id='DataTables_Table_0_paginate']/span/a")[-1].text)
but this produces an empty array. I am really not sure what I am doing wrong. Any help would be greatly appreciated. Thanks
You have to add expected condition to wait until the page loaded the data.
You can wait for visibility of element you are using and after that get it's text, like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[#id='DataTables_Table_0_paginate']/span/a")))
maxpage = int(browser.find_elements(By.XPATH,"//*[#id='DataTables_Table_0_paginate']/span/a")[-1].text)
You might want to try getattribute('textContent')
In your case:
maxpage=browser.find_element_by_xpath("(//*[#id='DataTables_Table_0_paginate']/span/a)[last()]").getattribute('textContent')

Python Selenium get text inside jacascript input

I have the bellow code and i want to extract the text in the parenthesis i.e '/team/barcelona/SKbpVP5K/'
<span class="team_name_span">
<a onclick="javascript:getUrlByWinType('/team/barcelona/SKbpVP5K/');">Barcelona</a></span>
Any way to do that?
best regards,
Nick
Below will print javascript:getUrlByWinType('/team/barcelona/SKbpVP5K/');. Assuming you define your driver and go the the url first. In my opinion trimming stings is a separate question but if needed I can add to this answer to help with that.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
(By.XPATH, "//span[#class='team_name_span']//a")))
text_i_want = element.get_attribute("onclick")
print(text_i_want)

Categories

Resources