Selenium无法点击按钮。

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

Selenium unable to click button

问题

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

它的元素是:

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

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

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.

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1424836534134.py in &lt;module&gt;
----&gt; 1 WebDriverWait(driver, 5).until(
      2     EC.element_to_be_clickable((By.XPATH, &quot;//button[text()=&#39;Open&#39;]&quot;))
      3 ).click()

~\Anaconda3\envs\webscraping\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     93             if time.monotonic() &gt; end_time:
     94                 break
---&gt; 95         raise TimeoutException(message, screen, stacktrace)
     96 
     97     def until_not(self, method, message: str = &quot;&quot;):

TimeoutException: Message: 
Stacktrace:
Backtrace:
	Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF7262B16C2+15186]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72624A212+827554]
	(No symbol) [0x00007FF725F0ED90]
	(No symbol) [0x00007FF725F52225]
	(No symbol) [0x00007FF725F523AC]
	(No symbol) [0x00007FF725F8E087]
	(No symbol) [0x00007FF725F71F8F]
	(No symbol) [0x00007FF725F44C3E]
	(No symbol) [0x00007FF725F8B513]
	(No symbol) [0x00007FF725F71D23]
	(No symbol) [0x00007FF725F43B80]
	(No symbol) [0x00007FF725F42B0E]
	(No symbol) [0x00007FF725F44344]
	Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72612C3B0+182752]
	(No symbol) [0x00007FF726000095]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72618A6EA+42362]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72618D425+53941]
	Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF7264A8AB3+1456595]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72625276A+861690]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF726257854+882404]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF7262579AC+882748]
	Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72626097E+919566]
	BaseThreadInitThunk [0x00007FFF40C77AD4+20]
	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()

driver.get("https://mokivezi.lt/leidiniai")
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']")))
openButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Open']"))
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()

driver.get(&quot;https://mokivezi.lt/leidiniai&quot;)
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;)))
openButton=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,&quot;//button[text()=&#39;Open&#39;]&quot;)))
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:"

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

"Try this possibly:"

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

&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;

try this possibly

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:

确定