PHP WebDriver 无法 select 用于登录的 iframe

PHP WebDriver cant select iframe for login

我正在制作将登录到“https://asia.nikkei.com/”的脚本。该脚本打开位于 iframe 中的登录 window。

但是我无法 select <inputs> 获取电子邮件和密码。当我尝试切换到 ID 为“piano-id-AciFE”的 iframe 时,使用此代码出现“没有这样的框架”错误:

$driver->switchTo()->frame("piano-id-AciFE");

我尝试了另一种解决方案:

$username = $driver->switchTo()->activeElement();

登录后window打开,但此解决方案也不起作用。

在这种情况下,有什么方法可以select登录输入吗?

id 属性的值是部分静态的(即 piano-id-)和部分动态的(即 AciFE)。当我访问 website 时,我发现 id 属性的值为 piano-id-IE9ZK

在这种情况下切换到所需的 you can refer to the preceeding(sibling) and you can use the following :

  • 使用 xpathfollowing:

    $element = $driver->findElement(WebDriverBy::xpath("//button[@aria-label='Close']//following::iframe[1]"));
    $driver->switchTo()->frame(element);
    
  • 使用 xpathfollowing-sibling:

    $element = $driver->findElement(WebDriverBy::xpath("//button[@aria-label='Close']//following-sibling::iframe[1]"));
    $driver->switchTo()->frame(element);
    

You can find a relevant detailed discussion in