kaliiiiiiiiii / Selenium-Driverless

undetected Selenium without usage of chromedriver
https://kaliiiiiiiiii.github.io/Selenium-Driverless/
Other
404 stars 52 forks source link

`ZeroDivisionError: Weights sum to zero, can't be normalized` when btn.click() #134

Open xrkk opened 6 months ago

xrkk commented 6 months ago

Code:

from selenium_driverless.sync import webdriver
from selenium_driverless.types.by import By

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
driver.get("https://www.zscaler.com/blogs")
btn = driver.find_element("//li[contains(@class,'rc-pagination-item-active')]/following-sibling::*[1]")

btn.click()  # exception

Tracelog:

C:\Users\Adminn\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\scripts\geometry.py:28: RuntimeWarning: invalid value encountered in scalar divide
  distance = np.linalg.norm(np.cross(v1, v2)) / np.linalg.norm(v1)
C:\Users\Adminn\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\scripts\geometry.py:33: RuntimeWarning: invalid value encountered in divide
  distances_normalized = distances / np.max(distances)
C:\Users\Adminn\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\scripts\geometry.py:47: RuntimeWarning: invalid value encountered in divide
  heatmap_probs /= np.sum(heatmap_probs)
NoneType: None
C:\Users\Adminn\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\types\webelement.py:484: UserWarning: couldn't get random point based on heatmap
  warnings.warn("couldn't get random point based on heatmap")
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[9], line 1
----> 1 btn.click()

File ~\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\sync\webelement.py:38, in WebElement.__getattribute__.<locals>.syncified(*args, **kwargs)
     37 def syncified(*args, **kwargs):
---> 38     return self._loop.run_until_complete(res(*args, **kwargs))

File ~\anaconda3\envs\py3\Lib\asyncio\base_events.py:653, in BaseEventLoop.run_until_complete(self, future)
    650 if not future.done():
    651     raise RuntimeError('Event loop stopped before Future completed.')
--> 653 return future.result()

File ~\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\types\webelement.py:424, in WebElement.click(self, timeout, bias, resolution, debug, scroll_to, move_to, ensure_clickable)
    421 if scroll_to:
    422     await self.scroll_to()
--> 424 x, y = await self.mid_location(bias=bias, resolution=resolution, debug=debug)
    425 if ensure_clickable:
    426     is_clickable = await self.is_clickable()

File ~\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\types\webelement.py:485, in WebElement.mid_location(self, bias, resolution, debug)
    483         traceback.print_exc()
    484         warnings.warn("couldn't get random point based on heatmap")
--> 485         point = centroid(vertices)
    486 else:
    487     point = centroid(vertices)

File ~\AppData\Roaming\Python\Python311\site-packages\selenium_driverless\scripts\geometry.py:85, in centroid(vertices)
     82 centroids = (vertices + polygon2) / 3.0
     84 # Get average of those centroids, weighted by the signed areas.
---> 85 return np.average(centroids, axis=0, weights=signed_areas)

File ~\AppData\Roaming\Python\Python311\site-packages\numpy\lib\function_base.py:550, in average(a, axis, weights, returned, keepdims)
    548     scl = wgt.sum(axis=axis, dtype=result_dtype, **keepdims_kw)
    549     if np.any(scl == 0.0):
--> 550         raise ZeroDivisionError(
    551             "Weights sum to zero, can't be normalized")
    553     avg = avg_as_array = np.multiply(a, wgt,
    554                       dtype=result_dtype).sum(axis, **keepdims_kw) / scl
    556 if returned:

ZeroDivisionError: Weights sum to zero, can't be normalized

Selenium-Driverless Version: 1.7

kaliiiiiiiiii commented 6 months ago

That basically means: Your element doesn't have any size and therefore can't be clicked on:)

EDIT

thanks @Lukas-drz for the provided additional information Looks like content size sometimes can be 0, with the element still being visible. see following: image

{'content': np.array([
       [673. , 224.76470947],
       [673. , 224.76470947],
       [673. , 240.76470947],
       [673. , 240.76470947]]), 
'padding': array([
       [665. , 216.76470947],
       [681. , 216.76470947],
       [681. , 248.76470947],
       [665. , 248.76470947]]), 
'border': array([
       [665. , 216.76470947],
       [681. , 216.76470947],
       [681. , 248.76470947],
       [665. , 248.76470947]]), 
'margin': array([
       [665. , 221.76470947],
       [676. , 221.76470947],
       [676. , 248.76470947],
       [665. , 248.76470947]]), 
'width': 16, 'height': 32}
Lukas-drz commented 6 months ago

I confirm that this bug is happening to me also and the element have a size and position. I do not understand why that happens.

kaliiiiiiiiii commented 6 months ago

I confirm that this bug is happening to me also and the element have a size and position. I do not understand why that happens.

huh I can give it a try. Generally tho, this occurs when the area of element is 0 I plan to refactor that stuff anyways

milahu commented 6 months ago

in my case, this happens when the element is not visible because the browser window is too small so the page switches to mobile layout (responsive web design)

this could also happen if the element is in some background layer and the click is swallowed by some foreground layer (css popup)

for debugging, disable headless insert await asyncio.sleep(99999999) and try to click that thing...

kaliiiiiiiiii commented 6 months ago

in my case, this happens when the element is not visible because the browser window is too small so the page switches to mobile layout (responsive web design)

this could also happen if the element is in some background layer and the click is swallowed by some foreground layer (css popup)

for debugging, disable headless insert await asyncio.sleep(99999999) and try to click that thing...

@milahu Yep, those are the cases, which I wouldn't consider a bug. However, https://github.com/kaliiiiiiiiii/Selenium-Driverless/issues/134#issuecomment-1859146706 clearly could be clicked on maually, & automated as well.

Way to go will be choosing tho most inner-layer, which still has a area to click on.