How to extract the value of the data-id attribute of the button control using Selenium - selenium

I have a button control on a web page as
<button class="btn btn-mini download-attachment pull-right width100px margin-bottom-1px" type="button" data-id="48156"><i class="icon-download"></i> Download</button>
I want to get the data-id associated with it. I am using selenium. Please suggest how can i fetch the data-id of this button control.

To print the value of the data-id attribute you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using C# and XPath:
Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(#class, 'download-attachment')][.//i[#class='icon-download']]"))).GetAttribute("value"));
Using Java and xpath:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'download-attachment')][.//i[#class='icon-download']]"))).getAttribute("data-id"));
Using Python and XPATH:
print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(#class, 'download-attachment')][.//i[#class='icon-download']]"))).get_attribute("data-id"))
Note : For python clients 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

Related

How to check if text is visible on web page

How does one check if this text (see picture) is visible to the user on the web page?
.is_displayed() clearly always returns true. Any alternatives?
The following always returns true (even when the text is not visible to the user)
driver.find_element(by=By.CSS_SELECTOR, value=".has-primary-color.has-text-color").is_displayed()
To probe if the text within <h3> tag is visible to the user on the web page or not you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h3.has-primary-color.has-text-color")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[#class='has-primary-color has-text-color']")))
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

How to handle the a tag in selenium

I am facing the problem for below tag in selenium
HTML Code is :
<a data-v-4sa1sads href='#' class='second'>Register</a>
I tried using LinkText, PartialLinkText, CSS selector, Xpath but it is always showing an error that element click is intercepted.
How to handle this.
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 java and linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Register"))).click();
Using java and XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='second' and text()='Register']"))).click();
Using python and LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Register"))).click()
Using python and CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.second[href]"))).click()
Note : For python clients 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
References
You can find a couple of relevant detailed discussions on ElementClickInterceptedException in:
ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python
ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python

NoSuchElementException: Message: no such element: Unable to locate element clicking span element using Selenium and Python

I am trying to click on a span class located inside a div class. Here's the HTML:
<div class="modal-content scrollbar">
<div class="block block-always-show action-black-box waves-effect">
<div class="icon xray-icon"></div>
<span class="txt">Xray - Test Product Research</span>
</div>
Still learning Selenium but here's what I've tried:
driver.find_element_by_xpath("//span[contains(#class, txt) and contains(text()='Xray - Test Product Research')]").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Xray - Test Product Research']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='txt' and contains(.,'Xray - Test Product Research')]"))).click()
I am getting these errors:
NoSuchElementException: Message: no such element: Unable to locate element:
and
TimeoutException: Message:
Thanks in advance and appreciate any help on a solution.
To click on the element with text as Xray - Test Product Research 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, "div.modal-content.scrollbar span.txt"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='modal-content scrollbar']//span[#class='txt' and contains(., 'Xray - Test Product Research')]"))).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
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
You're on the right path but it looks like your xpath's just need some slight tweaking.
Here are 2 examples that should find the element in question:
This xpath requires the exact text contained in the span tags:
//span[contains(#class, 'txt') and text() = 'Xray - Test Product Research']
This next xpath accepts snippets of text between the span tags:
//span[contains(#class, 'txt') and contains(text(), 'Xray')]
You may still need a small wait/time delay to allow for the elements to load on the page before trying to click on them

Another button that is not clicking

Below is the HTML of the button I am trying to click with various options but its not working:
<button data-ng-click="Question.setAnswer(button.value,button.attemptNext)" class="btn btn-sm btn-primary " type="button">No</button>
I tried the following
new WebDriverWait(driver, 0).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn btn-sm btn-primary' and #value='No']"))).click();
but its not working
TIA
Here is the console information.
i have been seeing the webdriver error before I added the line
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Jan 27, 2020 1:03:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
WebDriverException occured
No is the text, not the value. Use
new WebDriverWait(driver, 0).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn btn-sm btn-primary' and .='No']"))).click();
You seem to pretty close. The <button> element doesn't have a value attribute but have the innerText as No
Additionally, WebDriverWait for the duration of 0 isn't an ideal configuration and must be set to a positive value.
However, the desired element is a Angular element so to so to locate/interact with the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.btn-primary[data-ng-click*='attemptNext']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-sm btn-primary ' and text()='No']"))).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
Update
I don't see any such error from the stacktrace you have provided. As you are still unable to click on the element inducing WebDriverWait as an alternative you can use execute_script() as follows:
cssSelector:
element_css = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.btn-primary[data-ng-click*='attemptNext']")))
driver.execute_script("arguments[0].click();", element_css)
xpath:
element_xpath = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-sm btn-primary ' and text()='No']")))
driver.execute_script("arguments[0].click();", element_xpath)

How to click on the element with tooltip as Export as CSV using Selenium and Python

I'm trying to click an "export as csv" button on this webpage using selenium https://monitoringpublic.solaredge.com/solaredge-web/p/site/public?name=Alva%20Court%20E5&locale=en_GB#/dashboard (the button is next to "Power and Energy" title). Once I run the program, the site pops up but the download button is not clicked, resulting on Timeout Exception
However the code works with the following site that I found from another StackOverflow question https://www.rotowire.com/football/injury-report.php (although once I run the program and the site pops up, I have to manually accept the cookies in order for the file to be downloaded but that's another issue).
My question is why does the second link work but the first does not?
Here is the code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome("C:/Path/chromedriver.exe")
url = 'https://monitoringpublic.solaredge.com/solaredge-web/p/site/public? name=Alva%20Court%20E5&locale=en_GB#/dashboard'
browser.get(url)
button = wait(browser, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "is-csv")))
button.click()
browser.close()
To invoke click() on the element with tooltip text as Export as CSV you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[#class='x-panel-header-text' and text()='Power and Energy']//following::button[1]"))).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
Browser Snapshot:
For Power and Energy selector is #power_energy_panel button[class*=export].
For Comparative Energy is #se-comparative-energy-panel button[class*=export].
url = "https://monitoringpublic.solaredge.com/solaredge-web/p/site/public?name=Alva%20Court%20E5&locale=en_GB#/dashboard"
browser.get(url)
button = WebDriverWait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#power_energy_panel button[class*=export]")))
button.click()
The class name is incorrect. try with following class name,
button = wait(browser, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "se-button-btn export_csv_btn")))
button.click()