【问题标题】:Introducing Name in Search Field of Google Play Store using Selenium使用 Selenium 在 Google Play 商店的搜索字段中引入名称
【发布时间】:2023-01-20 09:51:22
【问题描述】:

我正在尝试在 Google Play 商店中搜索应用程序名称。为此,我必须单击右上角的镜头按钮使其出现,然后介绍名称。

我的方法如下,但我没有运气。我尝试使用其他选择器,但我不确定我是否选择了正确的选择器(我没有使用 Selenium 的经验)。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://play.google.com/store")
driver.find_element_by_css_selector('VfPpkd-Bz112c-Jh9lGc').click()
inputElement = driver.find_element_by_id("oA4zhb")
inputElement.send_keys('some app name')

有没有人成功地做到了这一点,可以提供指导,使该代码实现其目的?

提前致谢。

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    VfPpkd-Bz112c-Jh9lGc 不是搜索图标的有效 CSS 选择器,除非它是元素名称(它不是)。这实际上是一个类名,因此 CSS 选择器将是 .VfPpkd-Bz112c-Jh9lGc,请注意在开头添加的句点表示类名。如果您刷新页面几次,您将看到该类更改了名称,这告诉您它在定位器中不会有用。你需要找到永不改变的东西。理想情况下,它应该有 ID 或名称,但两者都没有。我会使用 CSS 选择器,button[aria-label="Search"]

    打开搜索框后,您需要找到它。您的代码正在查找“oA4zhb”的 ID,但如果您查看 HTML,您会发现 INPUT 没有 ID。您正在引用 jsname 属性。这可能稳定也可能不稳定,我不确定。我会使用 CSS 选择器 input[aria-label="Search Google Play"]

    将代码放在一起

    driver = webdriver.Firefox()
    driver.get("https://play.google.com/store")
    driver.find_element_by_css_selector('button[aria-label="Search"]').click()
    driver.find_element_by_css_selector('input[aria-label="Search Google Play"]').send_keys('some app name')
    

    【讨论】:

    • 我尝试了这个 sn-p 代码但没有用,但是,你给我的关于选择器的建议起到了作用,现在我可以做我需要的一切了。谢谢你的帮助我的朋友。
    【解决方案2】:

    使用交叉检查定位器..

    单击您可以使用的搜索图标

    By.xpath("//button/i[contains(.,'search')]")

    在搜索框中输入使用

    //input[@aria-label='Search Google Play']

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多