Selenium无法点击按钮。

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

Selenium unable to click button

问题

有一个按钮位于 https://mokivezi.lt/leidiniai 上,名为 "Open",您可以在目录图像下看到它。

它的元素是:

  1. <button data-href="open" aria-label="Open UAB &quot;Makveža&quot; - Pagrindinis Moki-vezi kaininis leidinys">Open</button>

使用 Python SeleniumEdge/Chrome 的 web 驱动程序,我无法点击该按钮。
我尝试切换到不同的 iframe,但仍然出现附加的错误。

  1. TimeoutException: Message:

我做错了什么?为了点击按钮,我尝试使用 WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Open'])).click()

英文:

there is a button in https://mokivezi.lt/leidiniai called "Open" you can see it under catalog image.

It's element is:
&lt;button data-href=&quot;open&quot; aria-label=&quot;Open UAB &amp;quot;Makveža&amp;quot; - Pagrindinis Moki-vezi kaininis leidinys&quot;&gt;Open&lt;/button&gt;

Using Python Selenium and Edge/Chrome webdrivers, I am unable to click that button.
I tried switching to different iframe, but still getting attached error.

  1. ---------------------------------------------------------------------------
  2. TimeoutException Traceback (most recent call last)
  3. ~\AppData\Local\Temp\ipykernel_1424836534134.py in &lt;module&gt;
  4. ----&gt; 1 WebDriverWait(driver, 5).until(
  5. 2 EC.element_to_be_clickable((By.XPATH, &quot;//button[text()=&#39;Open&#39;]&quot;))
  6. 3 ).click()
  7. ~\Anaconda3\envs\webscraping\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
  8. 93 if time.monotonic() &gt; end_time:
  9. 94 break
  10. ---&gt; 95 raise TimeoutException(message, screen, stacktrace)
  11. 96
  12. 97 def until_not(self, method, message: str = &quot;&quot;):
  13. TimeoutException: Message:
  14. Stacktrace:
  15. Backtrace:
  16. Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF7262B16C2+15186]
  17. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72624A212+827554]
  18. (No symbol) [0x00007FF725F0ED90]
  19. (No symbol) [0x00007FF725F52225]
  20. (No symbol) [0x00007FF725F523AC]
  21. (No symbol) [0x00007FF725F8E087]
  22. (No symbol) [0x00007FF725F71F8F]
  23. (No symbol) [0x00007FF725F44C3E]
  24. (No symbol) [0x00007FF725F8B513]
  25. (No symbol) [0x00007FF725F71D23]
  26. (No symbol) [0x00007FF725F43B80]
  27. (No symbol) [0x00007FF725F42B0E]
  28. (No symbol) [0x00007FF725F44344]
  29. Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72612C3B0+182752]
  30. (No symbol) [0x00007FF726000095]
  31. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72618A6EA+42362]
  32. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72618D425+53941]
  33. Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF7264A8AB3+1456595]
  34. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72625276A+861690]
  35. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF726257854+882404]
  36. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF7262579AC+882748]
  37. Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72626097E+919566]
  38. BaseThreadInitThunk [0x00007FFF40C77AD4+20]
  39. RtlUserThreadStart [0x00007FFF436CA371+33]

What wrong am I doing ? To click I tried using WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, &quot;//button[text()=&#39;Open&#39;]&quot;))).click()

答案1

得分: 1

The Open button is inside an iframe. you need to switch to iframe first, in order to access the element.

Use WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()

  1. driver.get("https://mokivezi.lt/leidiniai")
  2. WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src='//view.publitas.com/uab-makveza/pagrindinis-moki-vezi_kedainiu_parduotuve/?publitas_embed=embedded']")))
  3. openButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Open']"))
  4. driver.execute_script("arguments[0].click();", openButton)

浏览器快照: 点击“打开”按钮后

Selenium无法点击按钮。

英文:

The Open button is inside an iframe. you need to switch to iframe first, in order to access the element.

Use WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()

  1. driver.get(&quot;https://mokivezi.lt/leidiniai&quot;)
  2. WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,&quot;iframe[src=&#39;//view.publitas.com/uab-makveza/pagrindinis-moki-vezi_kedainiu_parduotuve/?publitas_embed=embedded&#39;]&quot;)))
  3. openButton=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,&quot;//button[text()=&#39;Open&#39;]&quot;)))
  4. driver.execute_script(&quot;arguments[0].click();&quot;, openButton)

Browser snapshot: After clicked on Open button

Selenium无法点击按钮。

答案2

得分: 0

代码部分不要翻译,以下是翻译好的内容:

"The problem is that the element you are trying to click on doesn't exist."

"The element that displays 'Open' is this:"

  1. <button data-href="open" aria-label="Open UAB &quot;Makveža&quot; - Pagrindinis Moki-vezi kaininis leidinys">
  2. Open
  3. </button>

"Try this possibly:"

  1. WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Open']"))).click()
英文:

The problem is that the element you are trying to click on doesn't exist

The element that displays "Open" is this

  1. &lt;button data-href=&quot;open&quot; aria-label=&quot;Open UAB &amp;quot;Makveža&amp;quot; - Pagrindinis Moki-vezi kaininis leidinys&quot;&gt;
  2. Open
  3. &lt;/button&gt;

try this possibly

  1. WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, &quot;//button[text()=&#39;Open&#39;]&quot;))).click()

huangapple
  • 本文由 发表于 2023年2月10日 16:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408504.html
匿名

发表评论

匿名网友

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

确定