Unable to open href link in selenium python - selenium

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

Related

Selenium - Having Problems finding a nz-select form element, when trying to click

I am currently trying to automate the input into a form on a website, but i cant seem to find a way to select the dropdown.
On website:
immosuche.degewo.de/de/properties/W1400-40660-0750-0902.html
You'll need to click on Kontaktieren.
In HTML:
I'm currently trying to find it by xpath this way:
driver.findElement(By.xpath("/html/body/el-root/div/el-listing-application/form/div[2]/div[1]/nz-form-item/nz-form-control/div/span/nz-select/div")).click();
But i always get this exception:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/el-root/div/el-listing-application/form/div[2]/div[1]/nz-form-item/nz-form-control/div/span/nz-select/div"}
(Session info: chrome=108.0.5359.126)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Does any one have any idea how i could click it without getting the Exeption?
Those elements are inside an iframe. To access elements inside it you need to switch into that iframe first.
The following code sample is working:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://immosuche.degewo.de/de/properties/W1400-40660-0750-0902.html"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".expose__header-functions a[href='#kontakt']"))).click()
time.sleep(3)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[formcontrolname='salutation']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//li[contains(.,'Herr')]"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "firstName"))).send_keys('Prophet')
wait.until(EC.element_to_be_clickable((By.ID, "lastName"))).send_keys('Mozes')
wait.until(EC.element_to_be_clickable((By.ID, "email"))).send_keys('mymail#mail.com')
wait.until(EC.element_to_be_clickable((By.ID, "formly_2_input_numberPersonsTotal_0"))).send_keys('5')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".form-actions [type='submit']"))).click()
The result screenshot is:
The request is sent.
When finished working inside the iframe don't forget to switch to the default content with:
driver.switch_to.default_content()
The <nz-select> element is within an iframe so you have to:
Induce WebDriverWait for the frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use the following locator strategies:
driver.get("https://immosuche.degewo.de/de/properties/W1400-40660-0750-0902.html");
new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#cookie-consent-submit"))).click();
new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//p[contains(., 'degewo Marzahner Wohnungsgesellschaft mbH')]//following::div[1]//span[text()='Kontaktieren']"))).click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src^='https://app.wohnungshelden.de/public/listings']")));
WebElement elem = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//nz-select[#nzplaceholder='Bitte auswählen']//div[#nz-select-top-control]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", elem);
Browser Snapshot:

How to extract the value of the data-id attribute of the button control using 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

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

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()