2

I can't locate this element.. I'm trying to un-check the history box and dl box (they're checked by default)

from selenium import webdriver
import time


chrome_path = r"C:\Users\Skid\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("chrome://settings/clearBrowserData")

driver.find_element_by_xpath("""//*[@id=delete-browsing-history-checkbox"]""") #unchecks history
driver.find_element_by_xpath("""//*[@id="delete-download-history-checkbox"]""") #unchecks dl history

This is the page source that someone wanted me to update.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" id="uber" class="" i18n-values="dir:textdirection;lang:language" dir="ltr" lang="en" i18n-processed=""><head>
<meta charset="utf-8" />
<title i18n-content="pageTitle">Settings - Clear browsing data</title>
<link id="favicon" rel="icon" type="image/png" sizes="16x16" href="chrome://theme/IDR_SETTINGS_FAVICON" />
<link id="favicon2x" rel="icon" type="image/png" sizes="32x32" href="chrome://theme/IDR_SETTINGS_FAVICON@2x" />

<link rel="stylesheet" href="chrome://resources/css/chrome_shared.css" />
<style>/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file. */

body {
  /* http://crbug.com/129406 --- horizontal scrollbars flicker when changing
   * sections. */
  overflow-x: hidden;
}

#navigation {
  height: 100%;
  left: 0;
  /* This is a hack to prevent the navigation bar from occluding pointer events
   * from the bottom scroll bar (which shows when one needs to horizontally
   * scroll). Corresponding padding-top to offset this is in uber_frame.css */
  margin-top: -20px;
  position: absolute;
  /* This value is different from the left value to compensate for the scroll
   * bar (which is always on and to the right) in RTL. */
  right: 15px;
  width: 155px;
  z-index: 3;
}

#navigation.background {
  z-index: 1;
}

#navigation.changing-content {
  -webkit-transition: -webkit-transform 100ms, width 100ms;
}

.iframe-container {
  -webkit-margin-start: -20px;
  -webkit-transition: margin 100ms, opacity 100ms;
  bottom: 0;
  left: 0;
  opacity: 0;
  position: absolute;
  right: 0;
  top: 0;
  z-index: 1;
}

.iframe-container.selected {
  -webkit-margin-start: 0;
  -webkit-transition: margin 200ms, opacity 200ms;
  -webkit-transition-delay: 100ms;
  opacity: 1;
  z-index: 2;
}

.iframe-container.expanded {
  left: 0;
}

iframe {
  border: none;
  display: block;
  height: 100%;
  width: 100%;
}
</style>

<script src="chrome://resources/js/cr.js"></script>
<script src="chrome://resources/js/cr/ui/focus_manager.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script>
<script src="chrome://resources/js/util.js"></script>

<script src="chrome://chrome/uber.js"></script>
<script src="chrome://chrome/uber_utils.js"></script>
</head>

<body>

<div id="navigation" data-width="155" class="changing-content background" style="transform: translateX(0px);"><iframe src="chrome://uber-frame/" name="chrome" role="presentation" tabindex="-1" aria-hidden="true"></iframe></div>

<div class="iframe-container" i18n-values="id:historyHost; data-url:historyFrameURL;" data-favicon="IDR_HISTORY_FAVICON" id="history" data-url="chrome://history-frame/" hidden="" aria-hidden="true"></div>
<div class="iframe-container" i18n-values="id:extensionsHost; data-url:extensionsFrameURL;" data-favicon="IDR_EXTENSIONS_FAVICON" id="extensions" data-url="chrome://extensions-frame/" hidden="" aria-hidden="true"></div>
<div class="iframe-container selected" i18n-values="id:settingsHost; data-url:settingsFrameURL;" data-favicon="IDR_SETTINGS_FAVICON" id="settings" data-url="chrome://settings-frame/" aria-hidden="false" data-title="Settings - Clear browsing data"><iframe name="settings" role="presentation" src="chrome://settings-frame/clearBrowserData" data-ready="true"></iframe></div>
<div class="iframe-container" i18n-values="id:helpHost; data-url:helpFrameURL;" data-favicon="IDR_PRODUCT_LOGO_16" id="help" data-url="chrome://help-frame/" hidden="" aria-hidden="true"></div>

<script src="chrome://chrome/strings.js"></script>
<script src="chrome://resources/js/i18n_template.js"></script>



</body></html>

3 Answers 3

2

driver.find_element_by_xpath is just looking for the checkbox and returning it as WebElement. You want to click on it to unchecked it

driver.find_element_by_xpath("""//*[@id="delete-browsing-history-checkbox"]""").click()

Also, you forgot apostrophes in the first xpath after @id=. It should be like in the example above.

Edit

You can try locating the checkbox by id

 driver.find_element_by_id("delete-browsing-history-checkbox").click()

Edit 2

The checkbox are inside iframe. You need to switch to it first

driver.switch_to.frame("settings") # switch to the iframe by name attribute
# driver.switch_to.frame(driver.find_element_by_name("settings")) # should also work
driver.find_element_by_id("delete-browsing-history-checkbox").click()
driver.switch_to.default_content() # switch back to main window
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, I've tried that. I shoulda told you that.. here's the error selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[@id=delete-browsing-history-checkbox"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@id=delete-browsing-history-checkbox"]' is not a valid XPath expression.
@nobodyskiddy see the edit to my question, I already answered on that :)
I still get an error :( selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="delete-browsing-history-checkbox"]"} (Session info: chrome=48.0.2564.109)
@nobodyskiddy I added another solution.
1

Can you add to your question what you get as body from selenium?

 driver.get("chrome://settings/clearBrowserData")
 driver.page_source

If I check the source code in Google Chrome of this page I get:

view-source:chrome://chrome/settings/clearBrowserData

<body>

<div id="navigation"><iframe src="chrome://uber-frame/" name="chrome" role="presentation"></iframe></div>

<div class="iframe-container"
    i18n-values="id:historyHost; data-url:historyFrameURL;"
    data-favicon="IDR_HISTORY_FAVICON"></div>
<div class="iframe-container"
    i18n-values="id:extensionsHost; data-url:extensionsFrameURL;"
    data-favicon="IDR_EXTENSIONS_FAVICON"></div>
<div class="iframe-container"
    i18n-values="id:settingsHost; data-url:settingsFrameURL;"
    data-favicon="IDR_SETTINGS_FAVICON"></div>
<div class="iframe-container"
    i18n-values="id:helpHost; data-url:helpFrameURL;"
    data-favicon="IDR_PRODUCT_LOGO_16"></div>

<script src="chrome://chrome/strings.js"></script>
<script src="chrome://resources/js/i18n_template.js"></script>

</body>

It might be necessary to find another way to do it, if your driver cannot see this node.

Edit

In the source code you posted as page_source returned from selenium, there isn't the node you are trying to find.

Comments

1

After doing a find_element_by... all you get is the element. You also need to have a .click() on that element.

Either:

elem = driver.find_element_by_xpath("""//*[@id=delete-browsing-history-checkbox"]""")
elem.click()

or:

driver.find_element_by_xpath("""//*[@id=delete-browsing-history-checkbox"]""").click()

Btw, you could just use find_element_by_id("delete-browsing-history-checkbox") in your case.

Also, I don't think selenium works on non-web pages. So chrome settings and Firefox's about:config pages (for example) don't work with selenium.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.