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

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:

Related

Click on button in selenium

I want to click on the list but they will give me time out error these is the page link https://www.s-ge.com/de/members-map
This is code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep
PATH="C:\Program Files (x86)\chromedriver.exe"
url='https://www.s-ge.com/de/members-map'
driver =webdriver.Chrome(PATH)
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#m-view-tabs__button is-activ"))).click()
Clicking on accept cookies is optional, it's not required.
You should launch the browser in full screen so that Selenium can have the list button in its view port.
If you observe even when you try to click manually on the list button, you are scrolling a little bit and then performing a click. You should automate that scrolling part as well.
Surprised to see that none of the answer are using CSS_SELECTOR.
Code:
driver_path = r'C:\\Users\\***\\***\\chromedriver.exe'
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get("https://www.s-ge.com/de/members-map")
list_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-target-tab='list']")))
driver.execute_script("arguments[0].click();", list_button)
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Could you try to use this XPath locator instead?
//button[contains(#data-target-tab, 'list')]
Either the locator is not correct, or the element is not clickable.
In case the element is not clickable, try to use js click.
element = driver.find_element_by_xpath("//button[contains(#data-target-tab, 'list')]")
def js_click(self, element):
driver.execute_script("arguments[0].click();", element)
Try with xpath
driver.find_element_by_xpath("//button[#class='m-view-tabs__button is-active']").click()

Selenium: Can't access cookie banner html

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

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

Unable to interact with the text field of website using selenium webdriver (using dynamic xpath)

I am trying to fill the input text field named Destination on a website and I am getting the error that "element not interactable". I searched and found that there could be temporary or permanent overlay so I tried using wait(implicit and explicit) but it didn't help.
**On Mozila I am getting the error: **
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
**And on chrome : ** org.openqa.selenium.ElementNotInteractableException: element not interactable
Here's the code:
public void check() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.phptravels.net/home"); //navigate to the particular url
driver.manage().window().maximize(); // maximizes of the window
Thread.sleep(1000);
driver.findElement(By.xpath("(//input[#type='text'][#class='select2-input'])[5]")).sendKeys("abc"); //search field
}
And yes I have imported all the prerequisites
To resolve your issue you can use selenium wait and action chain. ElementNotInteractableException is occurs due to overlay elements , element is not visible or clickable.
wait = WebDriverWait(driver, 10)
driver.get("https://www.phptravels.net/home")
selectElement=wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#id='s2id_autogen1']//input[#id='s2id_autogen2']")))
ActionChains(driver).move_to_element(selectElement).click().perform()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#id='s2id_autogen1']//input[#id='s2id_autogen2']"))).send_keys("Enter your Text")
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

Selenium issue with i frames

Enter a consumer no and select a region, go to the next page, download the pdf. These are the steps that I am trying to automate.
I am trying to download a PDF file which seems to be embedded in an iFrame is this the right way for that:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get(
"http://www.kseb.in/index.php?option=com_wrapper&view=wrapper&Itemid=813&lang=en")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(
(By.XPATH, "//iframe[contains(#src, 'ksebuser/orumabills/upload/billview/')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.XPATH, "//input[#class='userInputText']"))).send_keys("11230")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.ID, "office"))).send_keys("adoor")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.NAME, "b_submit_0"))).submit()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(
(By.XPATH, "//iframe[contains(#src, 'ksebuser/orumabills/upload/billview/bill_view_click.php')]")))
# print(driver.current_url)
# WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(
# (By.XPATH, "//iframe[contains(#src, 'ksebuser/orumabills/upload/billview/bill_view_click.php')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.ID, "download"))).click()
It is not working. Know any reason why?
You can use an XPath to locate the <iframe>:
iframe = driver.find_element_by_xpath("//iframe[contains(#src, 'ksebuser/orumabills/upload/billview/')]")
Then switch_to the <iframe>:
driver.switch_to.frame(iframe)
Here's how to switch back to the default content (out of the <iframe>):
driver.switch_to.default_content()
You must use driver.switch_to.frame(iframe) to interfere in the iframe.
But don't forget to go back using driver.switch_to.default_content().
the code below will work
driver = webdriver.Firefox()
driver.get('http://www.kseb.in/index.php?
option=com_wrapper&view=wrapper&Itemid=813&lang=en')
iframe = driver.find_element_by_id("blockrandom")
driver.switch_to.frame(iframe)
s = Select(driver.find_element_by_id('office'))
s.select_by_value('5617')
driver.find_element_by_id('t_consumer-no_5').send_keys('11230')
driver.find_element_by_xpath('/html/body/form/table/tbody/tr[4]/td[3]/input').click()
driver.switch_to.default_content()