9-Common exceptions

1、TypeError: ‘module’ object is not callable

Reason: The first letter of the browser is lowercase and needs to be changed to uppercase, such as driver = webdriver.Firefox()

2、“chromedriver” executable needs to be in path

Reason: There is no Chrome driver.
Solution: Need to download the driver and add it to the environment variable, or it has been downloaded, specify the driver path

If it still reports an error:

  • Add a path to the code, specify the path
  • Specify the path and report an error, check your own driver version
  • If you add a path, no error is reported, which proves that there is a problem with the environment variable

3. Click the element to report an error: Element is not clickable

Reason: Under the premise of successful positioning, it may be covered by other controls

① Use js to click

ele = driver.find_element_by_id(表达式)
driver.execute_script("$(arguments[0]).click()", ele)  
# 注:(arguments[0])就代表找到的ele元素

② Mouse event to click

ele = driver.find_element_by_id(表达式)
webdriver.ActionChains(driver).move_to_element(ele).click(ele).perform()

③ First click on the next element that will not generate an event, cancel the occlusion, and then click on the target element

4. The content of the text box cannot be cleared with clear

Phenomenon: After clearing the text box, the content of the text box is automatically filled.
Solution: For keyboard events, first select all the content of the text box and then delete the key to clear it.

5. The drop-down box is encapsulated by input and cannot be operated

Solution: First locate the input, then enter the content, through the keyboard event, select the drop-down box content

ele = driver.find_element_by_id(表达式)
ele.send_keys(Keys.DOWN)  # 向下
ele.send_keys(Keys.ENTER)  # 选中

6. Element not found: NoSuchElement

possible reason:

  • Interface changes, element positioning expressions are invalid, positioning fails (solution: repositioning)
  • The last operation was to enter a new page, but the operation was wrong, resulting in not entering the new page (solution: repositioning)
  • Successfully entered from the previous page, but did not switch to the new page (solution: switch to the corresponding tab)
  • Position the element without loading (solution: set the element to wait)
  • Embedded webpage (solution: switch to the corresponding embedded page)

7. Pycharm imports selenium and reports an error

possible reason:

  • Selenium is not installed in pycharm (solution: reinstall selenium)
  • Under the current project, there is selenium.py or a folder named selenium, which causes conflicts with the system package name
    (resolution: it is forbidden to use keywords to name any files)

8、SessionCreatedException … Chrome version 73

Reason: The driver does not match the browser.
Solution: Check the local driver version and download the corresponding chromedriver

9、WebDriverException:Message:Can not connect to the Service chromedriver.exe

Reason: The driver is blocked by the firewall.
Solution: Set the firewall to allow

10、WebDriverException:Message:Can not connect to the Service chromedriver

Reason: Cannot connect to chromedriver service. The code accesses the chromedriver service through the 127.0.0.1 ip, and the hosts file is not configured with 127.0.0.1 pointing to localhost.
Solution: Configure the local hosts file and add 127.0.0.1 localhost

11、RemoteDriverServerException

Reason: The remote server is abnormal. When the browser function combination/field is wrong, the server does not send a response.
Solution: Check the remote webdriver service

12. Can't find all kinds of

① NoSuchAttributeException

Reason: The element does not have this attribute, confirm whether the located element has the target attribute, or check the spelling of the word

② NoAlertPresentException

Reason: no alert pop-up window is found, observe the page to see if any pop-up window appears, or add waiting

③ NoSuchFrameException

Reason: The embedded webpage was not found, check element positioning, or word spelling

④ NoSuchWindowException

Reason: The window was not found. Is the window closed in advance, or check the spelling of the word, the list index, or the judgment condition

⑤ TimeOutException

Reason: In the display waiting or implicit waiting, the search element timed out, that is, the element cannot be found

13, the element operation is abnormal

① ElementNotVisibleException

Reason: the element is not visible abnormally, selenium cannot operate the hidden element.
Solution:

  • Try to sleep, it may change from hidden to visible
  • Check whether certain operations are required before it appears, and if so, perform these steps
  • Use js syntax to remove the attributes that affect the operation element,
    such as: document.querySelector(expression).removeAttribute(attribute)

② StaleElementException

Reason: The old element reference is abnormal. After the page is refreshed or the page jumps, the previously positioned element is used.
Solution: Reposition the element and operate

③ InvalidElementStateException

Reason: the element status is abnormal, the element is read-only, not clickable, etc.
Solution: wait or use js to remove the affected attribute

④ ElementNotSelectableException

Reason: the element cannot be selected, confirm whether the label is select

⑤ MoveTargetOutOfBoundsException

Reason: the position of the mouse event movement is not suitable, usually combined with the current state of the page, you can change the position

Guess you like

Origin blog.csdn.net/weixin_45128456/article/details/113976872