Selenium 和 Chromedriver - 元素不可交互

Selenium and Chromedriver - element not interactable

我已经使用 Selenium 几个星期了。在网站所有者最近更新代码之前,此脚本一直运行良好,现在我无法登录该网站。

Chrome Drivers和Selenium更新了,这个元素我怎么也学不会变得棘手

我试过添加:

$seleniumDriver.Manage().Timeouts().ImplicitlyWait((New-TimeSpan -Seconds 5))

那没用。

我也试过更改代码

$ChromeDriver.FindElementXPath("//*[@id='email']").Click 

$ChromeDriver.FindElement("email_username"").Click

那也没用。

此外,添加 Start-Sleep -seconds 15 也没有帮助。

必须有另一种方法可以使此用户名和密码框可点击。将不胜感激任何帮助。这是我目前所拥有的。


$workingPath = 'C:\selenium'

if (($env:Path -split ';') -notcontains $workingPath) {
    $env:Path += ";$workingPath"
}

Add-Type -Path "$($workingPath)\WebDriver.dll"
Add-Type -Path "$($workingPath)\WebDriver.Support.dll"

$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver

# Launch a browser and go to URL
$ChromeDriver.Navigate().GoToURL("https://fund.traderscentral.com/overview")

# Enter the username in the Username box
$ChromeDriver.FindElementXPath("//*[@id='email']").Click

$ChromeDriver.FindElementByXPath('//*[@id="email"]').SendKeys('b0rken@gmail.com')

# Enter the password in the Password box
$ChromeDriver.FindElementByXPath('//input[@name="password"]').SendKeys('12345678')

# Click on the Login button
$ChromeDriver.FindElementByXPath("//button[@type='submit']").Click();

# Cleanup
$ChromeDriver.Close()
$ChromeDriver.Quit()

恐怕 selenium 中的某些错误无法解决您的问题:

似乎在 div...

中没有看到输入字段

我已经通过使用 Actions 在 c# 中找到了解决方案,我不是 powershell 专家,但我想 powershell 中存在 Actions,C# 和 powershell 可以使用相同的库:

这是我在 C# 中的解决方案:我只是用 Actions 发送密钥,第一个选项卡转到电子邮件字段,我不知道为什么,但我必须在一行中写下所有发送密钥,希望对您有所帮助:

Actions actions = new Actions(driver);
actions.SendKeys(Keys.Tab).SendKeys("b0rken@gmail.com").SendKeys(Keys.Tab).SendKeys("12345678").Perform(); 

操作属于 OpenQA.Selenium.Interactions

在 powershell 中(未测试)

workingPath = 'C:\selenium'

if (($env:Path -split ';') -notcontains $workingPath) {
    $env:Path += ";$workingPath"
}

Add-Type -Path "$($workingPath)\WebDriver.dll"
Add-Type -Path "$($workingPath)\WebDriver.Support.dll"

$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver

# Launch a browser and go to URL
$ChromeDriver.Navigate().GoToURL("https://fund.traderscentral.com/overview")

#modif
# here 10sec adjust your wait time
[OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($ChromeDriver,[System.TimeSpan]::FromSeconds(10))
$wait.PollingInterval = 100

[void]$wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists([OpenQA.Selenium.By]::Id('email')))

[OpenQA.Selenium.Interactions.Actions]$actions = New-Object OpenQA.Selenium.Interactions.Actions ($ChromeDriver)
$actions.SendKeys([OpenQA.Selenium.Keys]::Tab).SendKeys("b0rken@gmail.com").SendKeys([OpenQA.Selenium.Keys]::Tab).SendKeys("12345678").Perform()

我看到一些添加 build(),不知道有什么不同,但你可以测试一下:

$actions.SendKeys([OpenQA.Selenium.Keys]::Tab).SendKeys("b0rken@gmail.com").SendKeys([OpenQA.Selenium.Keys]::Tab).SendKeys("12345678").Build().Perform()