Clicking on tab locator for selenium webdriver java - selenium

I am having trouble finding out the correct locator for clicking on the tab after logging into the a page
HTML:
<td title="Maintenance" id="c1_tab3" class="tabTab noselect tabSelectedTab" onclick="getcontrol( 'c1').setvalue(3);">Maintenance</td>
Test:
#Test
public void Case1() {
driver.navigate().to(URL);
//driver.findElement(By.linkText("Transcode Service")).click();
driver.findElement(By.id("c1_tab3")).click();
Error:
FAILED: Case1
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"c1_tab3"}
(Session info: chrome=70.0.3538.102)

can you try this xpath:
driver.findElement(By.xpath("//td[#id='c1_tab3' and contains(.,'Maintenance')]")).click();

Related

selenium webdriver find_element_by_xpath

I am trying to click on the accept button using xpath in www.simulator.betfair.com but it keeps giving me the error down below
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="onetrust-accept-btn-handler"]"}
(Session info: chrome=90.0.4430.85)
Any ideas? this is my code
from selenium import webdriver
url = r"C:\Users\salde\Desktop\chromedriver_win32 (1)\chromedriver.exe"
driver = webdriver.Chrome(executable_path=url)
driver.get('https://simulator.betfair.com/')
cookies = driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]')
cookies.click()

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

How to deal with parent and child iframes

I have a scenario where I 1st iframe (i.e parent iframe) which has one button on it and clicking on it another iframe gets open (child iframe). I am able to switch to Parent iframe but when I click on button and tries to interact with Child iframe I'm not able to do it. Can you suggest what should be the better approach to get this type of scenarios working?
My Script:
public class Iframe {
public static void main (String []args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://automation.cloudaccess.host/administrator");
driver.findElement(By.id("mod-login-username")).sendKeys("admin");
driver.findElement(By.id("mod-login-password")).sendKeys("admin#123");
driver.findElement(By.id("mod-login-password")).submit();
driver.findElement(By.linkText("Components")).click();
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Messaging"))).build().perform();
driver.findElement(By.linkText("New Private Message")).click();
driver.findElement(By.className("wf-editor-header")).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#id=\"jform_message_imgmanager\"]"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(#src,'&plugin=imgmanager')]")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"browser-actions\"]/a[#id=\"help\"]"))).click();
driver.switchTo().defaultContent();
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#id=\"mce_inlinepopups_50_ifr\"]")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"imgmanager.insert\"]/i[#class=\"icon-file\"]"))).click();
driver.quit();
}
}
Error:
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for frame to be available: By.xpath: //iframe[#id="mce_inlinepopups_50_ifr"] (tried for 20 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
at testScripts.Iframe.main(Iframe.java:51)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //iframe[#id="mce_inlinepopups_50_ifr"]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'vowellt4', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-24-generic', java.version: '1.8.0_171'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:517)
at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:513)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
... 1 more
Just after click on help button , you can try with this code :
driver.switchTo().defaultContent();
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src^='/administrator/index.php?option=com_jce&view=help&tmpl=component&lang=en&section=editor&category=imgmanager&']")));
You are using //iframe[#id=\"mce_inlinepopups_50_ifr\"] this xpath to switch to frame but the problem is the id is getting generated dynamically , so we do not know what will be the id every time we visit the page through automation.
I have simply converted that xpath to a valid and reliable css selector and it is working extremely good at my end.
In case you want to have xpath :
//iframe[contains(#src,'/administrator/index.php?option=com_jce&view=help&tmpl=component&lang=en&section=editor&category=imgmanager&')]
Hope this will help.
After clicking on the help button, you need to switch to the default content from the parent iframe and then need to navigate to the respective child frame.
Child Frame Xpath needs to be changed as below
Xpath: //*[#class='mceModalContainer']//div[#id='mce_inlinepopups_45_content']//iframe
Modified Working Code:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"browser-actions\"]/a[#id=\"help\"]"))).click();
driver.switchTo().defaultContent();
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//*[#class='mceModalContainer']//div[#id='mce_inlinepopups_45_content']//iframe")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='help-menu']//*[#id=\"imgmanager.insert\"]/i[#class=\"icon-file\"]"))).click();

Selenium: Unable to locate email type box, not within any iframe

I am trying to detect the login id and password field of a website : https://mretailstore.com/login but seems selenium is not able to locate the email type box. I have checked stackoverflow but didn't get any solution to this. Someone has used iframe because of what he/she was facing the same issue but here we have not incorporated any iframe.
The error I am getting is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .//*[#id='identity']
The code I am using:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\MI SERVICE\\Downloads\\geckodriver.exe");
FirefoxOptions capa = new FirefoxOptions();
capa.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capa);
driver.get("https://www.mretailstore.com/");
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();
driver.navigate().back();
driver.close();
It looks your xpath is correct only and this exception is happening before element rendering.So, Please add the some explicit wait after the page loading.
It is working for me with/without Explicit Wait.
Code:
driver.get("https://www.mretailstore.com/");
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleIs("Login"));
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();

Selenium - How to Click a Link by href value in WebDriver

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']")