Trouble locating a checkbox in an iframe with Selenium Web Driver - selenium

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

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

Couldn't locate the search bar

I need to locate a search bar to search some text listed in the below box and click on it. I tried below code but I couldn't perform the activity.
This code wasn't clicked the search bar:-
driver.findElement(By.xpath("//input[#class='Searchbar__search-field___2FQ0S search-input']")
Image of the HTML:
The desired elements are ReactJS enabled elements within a Modal Dialog so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-body#promotion-url-modal-body input.search-input[placeholder='Find a promotion...']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='modal-body' and #id='promotion-url-modal-body']//input[contains(#class, 'search-input') and #placeholder='Find a promotion...']"))).click();
Do you need to switch to a new frame before doing anything in the modal dialog? Do this:
driver.switchTo().activeElement();
And then try the following CSS locator:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#promotional-url-modal-overlay input[placeholder='Find a promotion...']"))).click();
Can you provide us with a link to the website you are trying to interact with? Chances are the element is within an iframe, in which case you need to switch to the iframe before you can interact with said element.
Make sure your window is maximised, as this can interfere with iframes:
driver.manage().window().maximize();
Then you will need to find the id of the iframe, using something like firebug
Once you have the id:
driver.switchTo().frame("xxxxxxxxx");
interact with said element within the iframe:
driver.findElement(By.xpath("html/body/a/img")).click();
class can be changed, you need to check HTML on failed step. As a workaround you can use more generic xpath:
driver.findElement(By.xpath("//input[contains(#class, 'search-input')]"))

Selenium WebDriver -- Unable to click on hyperlink, click goes to the other element

I am unable to click on Website hyperlink, the click goes to Recently used pages.
Tried with CSS locator of Website icon [which works in the lower environment as it doesn't have Recently used pages] reference in it.
Tried with XPath Locator[including custom XPath], still, the click goes to another item.
Tried name locator.
Used Actions class for clicking.
Allowed the page to load completely by using sleep and WebDriver wait.
Located the element and send Enter keys, still Recently used pages is clicked.
Tried to click it using coordinates.
Thought of ChromeDriver issue but the issue persists in Firefox too.
Tried below XPath:
html/body/div/div[2]/div[2]/div[1]/a/div
//div[2]/div/a/div
Code snippet:
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
(JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().x+")");
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
Actions actions = new Actions(driver);
actions.moveToElement(elementToClick);
actions.click().perform();
Actions builder = new Actions(driver);
builder.moveToElement(elementToClick, 40, 207).click().build().perform();
Result: It clicks on Recently Used Pages, and it yields a result of Recently used pages instead of Website.
UI Reference
Development Code Snippet
Hope It Helps you:
.//div[#id='box_2']/a/div[#class='icon siteadmin']/div[1]
Try the following:
driver.findElement(By.XPath(“//a[contains(#title, ‘Websites’)]”)).click()
If this doesn’t work use the above XPath in combination with one of the moves to element paths above and then use click.

How to select webElement present under form tag using selenium webdriver

How to click element present under Form tag in selenium web driver. using xpath, id, name it does not get recognized.refer screen shot
Here is a css selector:
.container-contact-info input[value='new']
Or an xpath
//*[#class='container-contact-info']//input[#value='new']
This might not work if you have a ton of these forms. If that's the case please update your question with the relevent information and I can adjust this answer.