Tag Archives: selenium

[Solved] Python Run Selenium Error: NameError: name ‘By’ is not defined

NameError: name 'By' is not defined

Reason: the by class does not have a package, and the system cannot recognize the by object

Solution:

Import by package:

from selenium.webdriver.common.by import By

———————————-Warm tips—————————————

Automatic package guide shortcut: Alt + ENTER

[Solved] Selenium Error: ElementClickInterceptedException

The error message is as follows:

The general meaning is that the current element is not clickable, but it does exist on the page, and it may be overwritten by loading
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button data-v-3916f4c2="" type="button" class="el-button el-button--primary" style="width: 100% ; height: 50px; border-radius: 10px; margin-top: 20px;">...</button> is not clickable at point (688, 398). Other element would receive the click: <div data-v- 3916f4c2="" class="login-top">...</div>

Solution 1: Force to wait for a few more seconds
# Can be forced to wait
import time
time.sleep()
Solution 2: Call js directly through selenium
js = driver.find_element(By.CSS_SELECTOR, ‘xxx’)
driver.execute_script(“arguments[0].click();”, js)

Display waiting:
The visibility_of_element_located used here is different from presence_of_element_located
visibility_of_element_located: After the element is found, the width and height of the element must be greater than 0 to execute;
presence_of_element_located: Execute directly after the element is found, maybe the element is covered by a mask, or the loading mask will cause you to be unable to click
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
try:
element = WebDriverWait(driver, 5).until(ec.visibility_of_element_located(loc))
except TimeoutException:
element.click()

Selenium Error: Message: unknown error: cannot find Chrome binary

The reason is that selenium cannot find the browser (note that this is the path of the browser, not the path of webdriver), so you need to specify the path of the browser as follows:

from selenium import webdriver

url = ‘ https://www.baidu.com ‘
options = webdriver.ChromeOptions()

#Here is the key
options.binary_location = “D:\Google\Chrome\Application\chrome.exe”

driver = webdriver.Chrome(executable_path=”D:\chromedriver_win\chromedriver.exe”, options=options)
driver.get(url)

Selenium: How to Solve Staleelementreferenceexception error (Three Methods)

In the process of automatic testing, the code has no errors, and the following errors can be encountered many times during running:
selenium. Common. Exceptions. Elementnotinteractableexception: Message: element not interactable

At present, I think of the following three solutions:
method 1:
using time. Sleep() can reduce the probability of this exception, but it is not 100% effective and wastes time.

Method 2:
use driver. Refresh() to refresh the page and then reposition this element.

Method 3:
in many cases during the test process, there will be deviations in the test results after refreshing the page. In this case, we can only use the method of capturing exceptions to avoid the current exceptions. That is, reposition the element when an exception occurs.

1    # time.sleep(2)
2    # driver.refresh()
3    try:
4        driver.find_element_by_id("details-button").click()
5        driver.find_element_by_id("proceed-link").click()
6    except:
7        driver.find_element_by_id("details-button").click()
8        driver.find_element_by_id("proceed-link").click()

Open chrome under selenium and solve the error

Open chrome under selenium and solve the error

 

 

The error is as follows:

[22516:20196:0704/024642.979:ERROR:install_util.cc(597)] Unable to read registry value HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken for writing result=2

 

 

The solution is as follows:

Just add a value named machinelevelusercloudpolicyenrollmenttoken (the data is empty) to the registry

HKLM_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmenToken

 

Transferred from: https://blog.csdn.net/hoddy355/article/details/80906596

 

You can also run the following commands through the administrator command:

Run “reg add HKLM \ software \ policies \ Google \ Chrome/V machinelevelusercloudpolicyenrollmenttoken/T reg” from the command line_SZ “, add the registry value prompted in the error message – confirmed, valid

 

 

Error reporting in Firefox using selenium in Python

python error in Firefox with selenium:

Traceback (most recent call last):
File “G:\python_work\chapter11\test_selenium_firefox.py”, line 10, in <module>
driver = webdriver.Firefox()
File “C:\Python34\lib\site-packages\selenium\webdriver\firefox\webdriver.py”, line 154, in __init__
keep_alive=True)
File “C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 140, in __init__
self.start_session(desired_capabilities, browser_profile)
File “C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 297, in execute
self.error_handler.check_response(response)
File “C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities

=============================

python version 3.4.3 , selenium version 3.4.3 , Firefox version 49.0.

 

The reason for the error is the version compatibility between python, selenium, Firefox , after upgrading Firefox to the latest version 55.0 the problem is solved.

Also selenium needs geckodriver when calling Firefox, download it and add it to the Firefox installation directory, and add the Firefox installation directory to the system environment variable Path.

=============================

Test code:

1 from selenium import webdriver
2 
3 driver = webdriver.Firefox()
4 driver.get("http://www.google.com")

seleniumImportError No module named selenium

Don’t panic in case of data tilt, teach you to easily obtain the slope of table tilt>>>

Method/step

What you can see is that the prompt of the running code is that the selenium file cannot be found

First of all, you need to download selenium files from the official website

After downloading, put the file into the Lib/site packages folder of the python installation path

Then use CMD to CD/enter the C disk

After entering the C disk, switch to the current site packages file

You can see that the current file is switched to selenium-3.11.0

After entering the file, execute the command Python setup. Py install

After waiting for the command to be executed, no error is reported, indicating that the installation is successful

After executing selenium related code, no error is reported

Under Ubuntu system, selenium opens the Firefox browser and prompts’ unable to find a matching set of capabilities. And message: connect

Environment.

1.ubuntu system

2.selenium 3.141.0

3.Firefox driver geckodriver-v0.24.0-linux32

To execute the script in Python.

from selenium import webdriver
browser = webdriver.Firefox()

Result tips: Message: Unable to find a matching set of capabilities

Traceback (most recent call last):
File “/home/yanner/seleniumtest/sousuo.py”, line 2, in <module>
browser = webdriver.Firefox()
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py”, line 174, in __init__
keep_alive=True)
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, line 157, in __init__
self.start_session(capabilities, browser_profile)
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

Solution: Update firefox browser</strong

1. Go to the official firefox website from your browser and download the latest version of firefox-67.0.4.tar.bz2

2. Go to the download directory and unzip the file. </strong

3. Open the terminal emulator, go to the download directory, and enter the install command line. <strong

root@yanner-VirtualBox:/home/yanner/download/firefox# sudo apt-get install firefox
After updating, Firefox 52.0.2

Execute the script again to open the browser normally:

from selenium import webdriver
browser = webdriver.Firefox()

However, opening the page fails with the message Message: connection refused:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get(“http://www.baidu.com”)
browser.find_element_by_id(“kw”).send_keys(“selenium”)
browser.find_element_by_id(“su”).click()
browser.quit()

After half a day of research, the driver geckodriver-v0.24.0-linux32 down version, the directory /usr/local/bin under the driver geckodriver deleted, downloaded geckodriver-v0.17.0-linux32 unzip copy to /usr/local/bin

Run the above script again and it opens normally!