Python的Selenium无法手动点击div,但可以手动点击该div。

huangapple go评论89阅读模式
英文:

Python selenium not able to click div manually able to click that div

问题

I am using python+selanium first time. Learning things for now :D.

I wrote script to login and pass username/password. Able to click on button and login.

But when there is section, where CLICK TO LOAD MORE paragraph has div which has click event and I can click on that div. Check below screen for the event and code view in chrome developer tools.

Python的Selenium无法手动点击div,但可以手动点击该div。

When I try to automate this step in python+selenium, it failed.

Code

...
...
67     load_more_p = WebDriverWait(
68         driver,
69         default_timeout).until(
70             EC.element_to_be_clickable(
71                 (By.XPATH,
72                  "//p[text()='CLICK TO LOAD MORE']/.."))).click()
...
...

Get the div by selecting CLICK TO LOAD MORE paragraph and its parent.

This gives error that, div is not clickable.

Traceback

Traceback (most recent call last):
  File "/Users/mycomputer/sam.py", line 67, in <module>
    load_more_p = WebDriverWait(
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 94, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="col-sm-12 has-text-centered cursor-pointer margin-double--top margin-double--bottom ng-tns-c178-9 ng-star-inserted">...</div> is not clickable at point (580, 752). Other element would receive the click: <div _ngcontent-rsk-c45="" class="columns banner has-text-white padding-double--left padding-double--right padding-half--top padding-half--bottom margin-none--bottom ng-tns-c45-2 ng-trigger ng-trigger-fadeInOut ng-star-inserted">...</div>
  (Session info: chrome=114.0.5735.106)
Stacktrace:
0   chromedriver                        0x000000010284ff48 chromedriver + 4226888
1   chromedriver                        0x00000001028484f4 chromedriver + 4195572
2   chromedriver                        0x000000010248cd68 chromedriver + 281960
3   chromedriver                        0x00000001024ce6e8 chromedriver + 550632
4   chromedriver                        0x00000001024cc638 chromedriver + 542264
5   chromedriver                        0x00000001024ca548 chromedriver + 533832
6   chromedriver                        0x00000001024c9918 chromedriver + 530712
7   chromedriver                        0x00000001024bdeec chromedriver + 483052
8   chromedriver                        0x00000001024bd734 chromedriver + 481076
9   chromedriver                        0x00000001024fec58 chromedriver + 748632
10  chromedriver                        0x00000001024bbf1c chromedriver + 474908
11  chromedriver                        0x00000001024bcef4 chromedriver + 478964
12  chromedriver                        0x000000010281159c chromedriver + 3970460
13  chromedriver                        0x00000001028156f0 chromedriver + 3987184
14  chromedriver                        0x000000010281b5b4 chromedriver + 4011444
15  chromedriver                        0x00000001028162fc chromedriver + 3990268
16  chromedriver                        0x00000001027ee1c0 chromedriver + 3826112
17  chromedriver                        0x0000000102832088 chromedriver + 4104328
18  chromedriver                        0x00000001028321e0 chromedriver + 4104672
19  chromedriver                        0x0000000102841f28 chromedriver + 4169512
20  libsystem_pthread.dylib             0x00000001955a026c _pthread_start + 148
21  libsystem_pthread.dylib             0x000000019559b08c thread_start + 8

is there any way to make sure what is clickable in manual also clickable in selenium automation ?

英文:

I am using python+selanium first time. Learning things for now :D.

I wrote script to login and pass username/password. Able to click on button and login.

But when there is section, where CLICK TO LOAD MORE paragraph has div which has click event and I can click on that div. Check below screen for the event and code view in chrome developer tools.

Python的Selenium无法手动点击div,但可以手动点击该div。

When I try to automate this step in python+selenium, it failed.

Code

...
...
67     load_more_p = WebDriverWait(
68         driver,
69         default_timeout).until(
70             EC.element_to_be_clickable(
71                 (By.XPATH,
72                  "//p[text()='CLICK TO LOAD MORE']/.."))).click()
...
...

Get the div by selecting CLICK TO LOAD MORE paragraph and its parent.

This gives error that, div is not clickable.

Traceback

Traceback (most recent call last):
  File "/Users/mycomputer/sam.py", line 67, in <module>
    load_more_p = WebDriverWait(
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 94, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/Users/mycomputer/src/.tox/unittest/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="col-sm-12 has-text-centered cursor-pointer margin-double--top margin-double--bottom ng-tns-c178-9 ng-star-inserted">...</div> is not clickable at point (580, 752). Other element would receive the click: <div _ngcontent-rsk-c45="" class="columns banner has-text-white padding-double--left padding-double--right padding-half--top padding-half--bottom margin-none--bottom ng-tns-c45-2 ng-trigger ng-trigger-fadeInOut ng-star-inserted">...</div>
  (Session info: chrome=114.0.5735.106)
Stacktrace:
0   chromedriver                        0x000000010284ff48 chromedriver + 4226888
1   chromedriver                        0x00000001028484f4 chromedriver + 4195572
2   chromedriver                        0x000000010248cd68 chromedriver + 281960
3   chromedriver                        0x00000001024ce6e8 chromedriver + 550632
4   chromedriver                        0x00000001024cc638 chromedriver + 542264
5   chromedriver                        0x00000001024ca548 chromedriver + 533832
6   chromedriver                        0x00000001024c9918 chromedriver + 530712
7   chromedriver                        0x00000001024bdeec chromedriver + 483052
8   chromedriver                        0x00000001024bd734 chromedriver + 481076
9   chromedriver                        0x00000001024fec58 chromedriver + 748632
10  chromedriver                        0x00000001024bbf1c chromedriver + 474908
11  chromedriver                        0x00000001024bcef4 chromedriver + 478964
12  chromedriver                        0x000000010281159c chromedriver + 3970460
13  chromedriver                        0x00000001028156f0 chromedriver + 3987184
14  chromedriver                        0x000000010281b5b4 chromedriver + 4011444
15  chromedriver                        0x00000001028162fc chromedriver + 3990268
16  chromedriver                        0x00000001027ee1c0 chromedriver + 3826112
17  chromedriver                        0x0000000102832088 chromedriver + 4104328
18  chromedriver                        0x00000001028321e0 chromedriver + 4104672
19  chromedriver                        0x0000000102841f28 chromedriver + 4169512
20  libsystem_pthread.dylib             0x00000001955a026c _pthread_start + 148
21  libsystem_pthread.dylib             0x000000019559b08c thread_start + 8

is there any way to make sure what is clickable in manual also clickable in selenium automation ?

答案1

得分: 1

To click on the clickable element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[text()='CLICK TO LOAD MORE']"))).click()

  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

英文:

To click on the clickable element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[text()='CLICK TO LOAD MORE']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

huangapple
  • 本文由 发表于 2023年6月15日 05:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定