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
Related
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
I've been trying to access the html code of cookie banners using Selenium. For some websites, I can see the cookie banner html in the Firefox Web-Inspector, however, I cannot access it via Selenium.
For example https://faz.net. Here, driver.page_source does not contain the html code of the cookie banner and I also can't access it's elements via driver.find_elements (e.g. the "ZUSTIMMEN" - button. "zustimmen" means "to accept").
What I've tried so far:
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(20)
driver.get("https://faz.net")
print(driver.page_source) # page source does not contain the button "ZUSTIMMEN"
print(driver.find_elements_by_xpath('//button[text()="ZUSTIMMEN"]'))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[text()=ZUSTIMMEN"]'))))
What am I doing wrong?
That button ZUSTIMMEN is in iframe. You need to switch the driver focus to iframe like below :
driver.get("https://faz.net")
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id^='sp_message_iframe']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='ZUSTIMMEN']"))).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
once you are done with iframe, you can switch to default content like this :
driver.switch_to.default_content()
That element is inside an iframe.
You have to switch to the iframe in order to access the element.
Like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://faz.net")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(#title,'SP')]")))
Now you can click on the cookie button in order to close it with
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[title='ZUSTIMMEN']"))).click()
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
I have a array called accounts which gets all the href's i want, i then want to open each of these, i have tried the following code
accounts = self.driver.find_elements_by_xpath("//a[contains(#href, '/signin?')]")
for account in accounts:
self.driver.get(account)
time.sleep(3)
But returns
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string
(Session info: chrome=80.0.3987.132)
You are fetching the list of web elements, so you need to first fetch the href attribute from those web elements and then hit them.
You can do it like:
accounts = self.driver.find_elements_by_xpath("//a[contains(#href, '/signin?')]")
for account in accounts:
self.driver.get(account.get_attribute("href"))
time.sleep(3)
To open the href, instead of the WebElement you need to invoke get() passing the href attribute inducing WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
accounts = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a[href*='/signin?']")))]
for account in accounts:
self.driver.get(account)
Using XPATH:
accounts = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[contains(#href, '/signin?')]")))]
for account in accounts:
self.driver.get(account)
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
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()