首页 > 解决方案 > 使用 selenium 访问元素

问题描述

请帮助我了解如何使用硒访问“文件上传”(谷歌驱动器)

在此处输入图像描述

登录谷歌驱动器后,点击“新建”会弹出一个弹出窗口,其中包含“文件夹”、“文件上传”、“文件夹上传”等选项,

我的问题是:如何访问“文件上传”(没有在浏览器中检查该元素的选项)。

谢谢

标签: javaselenium

解决方案


如果您想更好地上传文件,请使用https://developers.google.com/drive/api/v3/manage-uploads

按钮的 Xpath 是:

.//div[.='File upload']

HTML:

<div class="h-v" role="menuitem" id=":2q" style="user-select: none;">
    <div class="h-v-x">
      <span class="h-v-c a-v-c a-v-c-mh">
        <div>
          <svg class="a-s-fa-Ha-pa" x="0px" y="0px" width="20px" height="20px" viewBox="0 0 20 20" focusable="false" fill="#000000">
            <path d=""></path>
          </svg>
        </div>
      </span>
      <span class="a-v-j">
        <span class="a-v-ff">
          <div class="a-v-T">File upload</div>
        </span>
      </span>
    </div>
</div>

代码可能是:

driver.findElement(By.cssSelector("button[aria-label='New']")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.xpath(".//div[.='File upload']"))).click();

推荐阅读