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
Related
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();
I am trying to clear browser cache, for which i need to click on clear data button of browser setting popup, but, i am not able to write xpath for the button on chrome browser
i have tried inspecting the element to find out if the button is on a iframe but its not in iframe, so i have decided to try it with an with out iframe snippet, either of ways the element is not traces out in dom
public void clearBrowserCache() throws InterruptedException{
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(2000);
System.out.println(driver.getWindowHandles());
String windowIds=driver.getWindowHandle();
// driver.switchTo().frame(windowIds);
driver.findElement(By.cssSelector(
[id=clearBrowsingDataConfirm]")).click();
}
Expected is that i should be able to click on the clear data button
Actual is that i am not able to find out the xpath for of the emlement
Depending on which version of chrome you are using, this could work:
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
However the /deep/ combinator is deprecated, so it may not work on newer Chrome's versions.
I answered how to reach inside the Shadow DOM in an other question.
You can read the whole thing at the link, but the basics are you create a "starting point" WebElement at the Shadow DOM via JavaScript, then all future look-ups reference it:
WebElement button = startingPoint.findElement(By.cssSelector("..."));
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");
Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver
In my Ajax application, we have dropdown + link (Customize) together in div and i want to click to Customize link. I have locator and which was working fine for Customize link with old selenium but it doesn't with latest web driver. Can anyone please point me the problem or suggest something to make it work?
Expected:
Clicking to Customize link should open respected option (it actually opens dialog).
Actual:
Below locator clicks to dropdown button instead of Customize link due to such a complex page DOM which has no actual href or anchor tag.
Locator:
css=div[id$='_repeatDesc'][class='FakeAnchor']
Html:
<div id="zcs1_repeatDesc" class="FakeAnchor" style="cursor: pointer;">Customize</div>
Code:
webDriver().findElement(By.cssSelector("div[id$='_repeatDesc'][class='FakeAnchor']")).click();
I think your locator is not unique, may be it is locating dropdown element that's why it clicks to dropdown button instead of Customize link.
You should try using By.xpath() with text() node to locate this element as below :-
webDriver().findElement(By.xpath(".//div[text() = 'Customize']")).click();
Or As I'm seeing in HTML element has id attribute, if it's unique I'd to locate desire element and it's not being changed dynamically, you can try also using By.id() as below :-
webDriver().findElement(By.id("zcs1_repeatDesc")).click();
Edited :- If you want to click using JavascriptExecutor try as below :-
((JavascriptExecutor)driver).executeScript("arguments[0].click()", webDriver().findElement(By.xpath(".//div[text() = 'Customize']")));
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();