selenium handles invalid element positioning clicks

  • 2021-06-28 13:05:26
  • OfStack

In the WEB automated testing process, such problems are often encountered:

The element is located, but the click is invalid?Someone might ask, how do you tell if an element is positioned? That's a good question to judge.

1. Highlight elements


  self.driver.execute_script(
   "arguments[0].setAttribute('style', arguments[1]);",
   element,
   "border: 2px solid red;" # Frame border:2px; red gules 
  )

arguments[0] is a parameter that can be interpreted as string formatting in python.For example,'{}{}'. format ('aaa','bbbb')

2.There is no error in executing the click process.

No errors such as 1 element not found will be reported.

How to tell if an element is located and the click is unsuccessful (here 1, 1 usually makes a click, but we intuitively judge if the click is unsuccessful)

Now, let's take a look at how to solve this problem.

Solve the problem:

1.Analysis 1, the default selenium click operation is point element 0, 0 coordinates, then we can add 5 on the element x, y coordinates, so that the click operation is 1 point inward.

Import package ActionChains,


from selenium.webdriver.common.action_chains import ActionChains

ele = self.find_element(*loc)
ActionChains(driver).move_to_element(ele).move_by_offset(5,5).click().perform()

With the code above, locate the element, move to it, offset x, y 5,5 on the element, and click.

Notice the final perform ().To add this.Otherwise it will not take effect.

2. Click on the element through js.

Location can be done through dom or jquery.Then click Action.This is not specified here.You can find a bride.

3. Code can be analyzed, and if there is a direct call to the function, it can be called directly through js.

To illustrate, this method is generally not feasible.Because the js front-end code compresses the encryption.So what we see when we go online is compressed.


Related articles: