Using Selenium Chrome Driver to search elemets by xpath is not working - selenium

I am running my Cucumber Test using Selenium and using Chrome driver. I am trying to get a div element with xpath but it is not working.
It is the html element:
<div class="row" id="headerspacing"></div>
And it is my test code:
WebElement div = agent.findElement(By.xpath(".//*[#id='headerspacing']"));
Assert.assertNotNull(div);
But I am getting an error message with the element:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='headerspacing']"}
Update: new tab window
In the previous steps declared in my Cucumber tests Selenium hit a link (tag element a) and it step is opening a new tab windows. I thought Selenium was searching into that page and it is the problem: It was looking into the current page instead of new tab.
So maybe my question:
How do I search or move my driver in order to do a search into the new open tab windows?

If the div is in a new window (tab), you must switch to that window before you can locate any elements on it. So, before trying to find the element, you want to do:
switchTo().window("windowName");

Related

Interact with pseudo-elements with Selenium Webdriver?

I am working with Selenium Webdriver with Java.
And I was trying to interact with anchor tag which is enclosed as pseudo element ::before
But I am unable to interact with anchor element.
Here is the screenshot of the HTML structure.
With
JavaScriptExecutor, I understand, we can fetch the propertyValue using window.getComputedStyle().getPropertyValue() but I am not sure, how to interact with <a> element and execute a Click.
Initially, I attempted to click on the Anchor Element without considering the pseudo element as simple Element Interaction.
To fetch the Element:
private By tabRawView_By_CSS = By.cssSelector("[tabid='raw-view'][role='tab']");
But this piece is not throwing any error but it is also not clicking on the element.
Then I thought of using JavaScriptExecutor and was trying to run first in Developers Tool as below image but couldn't find suitable options.
Can anyone please suggest?
If it is Java bindings and all you want to do is to click on an achor tag which has Raw view as a text.
You could try with ExplicitWaits :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Raw View"))).click();

Unable to locate element | xPath | Selenium Webdriver

I am trying to reference and then click an element on a web page.
This is the web page: https://www.facebook.com/settings
This is the element:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
It's a DIV which contains the word "Notifications"
We should be able to reference with a simple xPath. Such as //div[contains(text(), 'Notifications')]
However, it doesn't seem to work.
A chrome extension I use says the element doesn't exist.
My code can't find the element.
Here is my Java code using Selenium web driver.
// Navigate to page
obj.driver.get("https://www.facebook.com/settings");
// Write to console where we are
System.out.println(obj.driver.getCurrentUrl());
// Wait | Plenty of time for the page to load
Thread.sleep(5000);
obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]")).click();
Here is the error:
It is so very strange! Any ideas on why I can't reference the element, or why the xPath doesn't exsist.
Your element is within an iframe.
If you scroll up from where you are you'll see this:
For selenium you need to switch frames in order to access the elements within them
I've not tried it for your site yet - but you switch frames with:
driver.switchTo().frame(1); // by index
//<or>
driver.switchTo().frame("id of the element"); //by id
//<or>
driver.switchTo().frame(element); // be element
Then, when ready switch back to the main page/frame with:
driver.switchTo().defaultContent();
Have a look at the frames section of the selenium docs here

Unable to locate addons button in firefox android app using selenium java with appium

Selenium is unable to locate the addons buttons on firefox android app. I have tried both id and xpath locator.
Here is the code snippet:
// driver is an instance of AndroidDriver
driver.navigate().to("about:addons");
Thread.sleep(5000);
WebElement mainLayout = driver.findElementById("org.mozilla.firefox:id/main_layout");
System.out.println("#" + mainLayout.getText());
WebElement addonsList = driver.findElementById("addons-list");
System.out.println("#" + addonsList);
MobileElement randomUserAgent = (MobileElement) addonsList.findElement(By.xpath("//android.widget.Button[5]"));
System.out.println(randomUserAgent);
I am able to open the addons page, but after that I am getting following error (not able to find element with id "addons-list"):
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
Can anyone help me to locate and clcik on the addons button.
Thanks
I would rather to change user-agent at mobile firefox another way: by changing preferences of browser by hand:
Open a new tab and enter the following text in the address bar:
about:config
Enter the following text in the search box:
general.useragent.overridepreference
If you don't have the value general.useragent.overridepreference, create it yourself. Right-click on the about:config page, and go to New -> String in the context menu.
More detail here.

Unable to identify a static dropdown

I am trying to automate a webpage where I am unable to identify a static drop down.
<select name = "homeURL" tabIndex = "4" class = "storeId" >..</select>
I tried to identify it using below mentioned code using Select class
Select dropdown = new Select(driver.findElement(By.name("homeURL")));
dropdown.selectByIndex(2);
I am getting the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with name == homeURL (WARNING: The server did not provide any stacktrace information)
It could be a various amount of different factors. Such as the web driver, iframe, or even how long it takes for the page to render. Please consider the following links:
Web Driver: Selenium InternetExplorerDriver unable to find element that is clearly there
Iframe:
Selenium WebDriver: unable to locate element inside iframe using TinyMCE editor
Page rendering issue:
Selenium Unable to Find Element
Please consider looking at the version of your web driver, selenium, and installed browsers.

Trouble locating a checkbox in an iframe with Selenium Web Driver

I'm trying to locate a checkbox in an iframe in a Javascript based website using Web Driver and python. I've tried locating by ID and XPATH and neither seem to work since I must be looking in the wrong frame. The checkbox is visible on the page to the user and selenium IDE also seems to come up with the same answer as me, but it still results in 'NoSuchElementException: Message: u'The element could not be found'
html for checkbox: input type="checkbox" onclick="CE.CESECUR.onClickFullSecurity()" id="cefullsecure"
selenium code:
_settings_ssl_locator = (By.XPATH, ".//*[#id='cefullsecure' and onclick='CE.CESECUR.onClickFullSecurity()']")
def click_settings_enable_ssl(self):
self.selenium.find_element(*self._settings_ssl_locator).select()
I think you you would need to switch to the frame first and then locate the element, do the task/action on the elements present on the frame and switch back to the previous frame.
WebElement checkBoxframe = driver.findElement(By.tagName("enter frame name here"));
driver.switchTo().frame(checkBoxframe);
//write your selenium code here for checkbox
driver.switchTo().defaultContent();