I have this piece of code
<a href="/iot/apply/device.do" id="subMenu1" class="fortification55"
onclick="$('#deviceinfo').hide()">Apply</a>
I am trying to link by href using
getDriver().findElement(By.name("subMenu1")).click();
But i got this error
org.openqa.selenium.NoSuchElementException: Unable to find element with name == subMenu1 (WARNING: The server did not provide any stacktrace information)
As the element is having the onclick Event, the WebElement is a JavaScript enabled element. So to invoke click() on the element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector and only href attribute:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/iot/apply/device.do']"))).click();
Using xpath and only href attribute:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#href='/iot/apply/device.do' and text()='Apply']"))).click();
Using a canonical cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.fortification55#subMenu1[href='/iot/apply/device.do']"))).click();
Using a canonical xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='fortification55' and #id='subMenu1'][#href='/iot/apply/device.do' and text()='Apply']"))).click();
Note : You have to add the following imports :
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
References
You can find a couple of relevant discussions on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element
Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[#id='login-email']
the following code should work :
By.xpath("//a[#href='/iot/apply/device.do']")
Related
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:
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 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 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
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