无法在由webdriver控制的Chrome中点击按钮。

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

Unable to click on a button when Chrome controlled by webdriver

问题

I use a mix of VBA and now Selenium to automate some of my office tasks. 我使用VBA和Selenium混合自动化一些办公任务。

I have a list of serial numbers (SN) for some devices in Excel, and I am trying to write a script to open Chrome at a Dell support page, input the SN into a field to check the warranty, and return the expiration date into Excel. 我在Excel中有一串设备的序列号(SN),我尝试编写一个脚本,在Dell支持页面打开Chrome,将SN输入到字段中以检查保修情况,并将到期日期返回到Excel。

I cannot bypass the click button operation, usually it would work just fine, alternatively I would use submit, but this time I cannot find the solution. 我无法绕过点击按钮的操作,通常它应该能正常工作,或者我可以使用提交,但这次我找不到解决方法。

I know there are APIs to use with Dell that will return the expiration date but I don't want to use the API key for some reasons. 我知道有与Dell一起使用的API可以返回到期日期,但由于某些原因,我不想使用API密钥。

Once I manage to sort out the click, I will pull SN values into an array and loop through instead of using ActiveCell. 一旦我成功解决点击问题,我将将SN值提取到一个数组中,而不是使用ActiveCell。

Thanks for your suggestions, Karel 感谢您的建议,Karel

Sub CheckDellWarranty()

' Set up Selenium
Dim driver As New WebDriver
driver.Start "chrome"

' Navigate to the specific URL
driver.Get "https://www.dell.com/support/home/fr-fr?app=products"

' Wait for Chrome to load
driver.wait 3000

' Get the value of the currently selected cell in Excel
Dim cellValue As String
cellValue = Application.ActiveCell.value

' Insert the value into the specified field using XPath
Dim inputElement As WebElement
Set inputElement = driver.FindElementByXPath("/html/body/div[5]/div/div[3]/div[1]/div/div/div[2]/div[1]/div[1]/div[1]/div/div/form/input")
inputElement.SendKeys cellValue

' Enable the search button
driver.ExecuteScript "document.getElementById('btn-entry-select').disabled = false;"

' Find the search button by its ID
Dim searchButton As WebElement
Set searchButton = driver.FindElementById("btn-entry-select")

EVEN THOUGH IT CLICKS, THE BUTTON JUST SPINS AND WILL NOT SEARCH. WHEN I OPEN CHROME MANUALLY, THE CLICK ACTION ACTUALLY FINDS THE SN.

If Not searchButton Is Nothing Then
    ' Use JavaScript to trigger the click event on the search button
    driver.ExecuteScript "arguments[0].click();", searchButton
Else
    MsgBox "Failed to locate the button."
End If
Stop

End Sub

英文:

I use a mix of VBA and now Selenium to automate some of my office tasks. I have a list of serial numbers (SN) for some devices in Excel, and I am trying to write a script to open Chrome at a Dell support page, input the SN into a field to check the warranty, and return the expiration date into Excel. I cannot bypass the click button operation, usually it would work just fine, alternatively I would use submit, but this time I cannot find the solution.

I know there are APIs to use with Dell that will return the expiration date but I don't want to use the API key for some reasons.

Once I manage to sort out the click, I will pull SN values into an array and loop through instead of using ActiveCell.

Thanks for your suggestions,
Karel

Sub CheckDellWarranty()

' Set up Selenium
Dim driver As New WebDriver
driver.Start "chrome"

' Navigate to the specific URL
driver.Get "https://www.dell.com/support/home/fr-fr?app=products"

' Wait for Chrome to load
driver.wait 3000

' Get the value of the currently selected cell in Excel
Dim cellValue As String
cellValue = Application.ActiveCell.value

' Insert the value into the specified field using XPath
Dim inputElement As WebElement
Set inputElement = driver.FindElementByXPath("/html/body/div[5]/div/div[3]/div[1]/div/div/div[2]/div[1]/div[1]/div[1]/div/div/form/input")
inputElement.SendKeys cellValue

' Enable the search button
driver.ExecuteScript "document.getElementById('btn-entry-select').disabled = false;"

' Find the search button by its ID
Dim searchButton As WebElement
Set searchButton = driver.FindElementById("btn-entry-select")

EVEN THOUGH IT CLICKS, THE BUTTON JUST SPINS AND WILL NOT SEARCH. WHEN I OPEN CHROME MANUALLY, THE CLICK ACTION ACTUALLY FINDS THE SN.

If Not searchButton Is Nothing Then
    ' Use JavaScript to trigger the click event on the search button
    driver.ExecuteScript "arguments[0].click();", searchButton
Else
    MsgBox "Failed to locate the button."
End If
Stop

End Sub

答案1

得分: 0

不好意思,你提供的内容中有一些HTML和代码,我将为你提供翻译:

"It doesn't seems that the <kbd>Search</kbd> button is ever disabled. The &lt;button&gt; element have 2 descendants:

  • a &lt;div&gt; with style=&quot;display: none;
  • a &lt;span&gt; with text Search

You can invoke the click on the &lt;span&gt; with text Search using either of the following locator strategies:

  • Using FindElementByCss:

    driver.FindElementByCss(&quot;button#btn-entry-select span&quot;)
    
  • Using FindElementByXPath:

    driver.FindElementByXPath(&quot;//button[@id=&#39;btn-entry-select&#39;]//span&quot;)"
    

更新

等效的工作Python代码片段:

driver.get("https://www.dell.com/support/home/en-in?app=products")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='entry-main-input-home']"))).send_keys("Latitude 10")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btn-entry-select span"))).click()

请注意,翻译的内容中保留了HTML和代码部分,只翻译了文字内容。

英文:

It doesn't seems that the <kbd>Search</kbd> button is ever disabled. The &lt;button&gt; element have 2 descendants:

  • a &lt;div&gt; with style=&quot;display: none;
  • a &lt;span&gt; with text Search

You can invoke the click on the &lt;span&gt; with text Search using either of the following locator strategies:

  • Using FindElementByCss:

    driver.FindElementByCss(&quot;button#btn-entry-select span&quot;)
    
  • Using FindElementByXPath:

    driver.FindElementByXPath(&quot;//button[@id=&#39;btn-entry-select&#39;]//span&quot;)
    

Update

Equivalent working Python code snippet:

driver.get(&quot;https://www.dell.com/support/home/en-in?app=products&quot;)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;input[name=&#39;entry-main-input-home&#39;]&quot;))).send_keys(&quot;Latitude 10&quot;)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;button#btn-entry-select span&quot;))).click()

huangapple
  • 本文由 发表于 2023年6月8日 18:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430845.html
匿名

发表评论

匿名网友

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

确定