如何使用 Xpath 在 Google 主页上找到搜索框

How to locate the search box on Google Home page using Xpath

提问人:BRAJESH KUMAR 提问时间:7/29/2023 最后编辑:undetected SeleniumBRAJESH KUMAR 更新时间:8/11/2023 访问量:67

问:

我已经从开发人员工具xpath中输入了正确的内容,但它不是macthing元素。如何找到 xpath 或其他定位器的谷歌搜索框。

HTML格式:

<input id="input" type="search" autocomplete="off" spellcheck="false" role="combobox" ariacontrols="matches" placeholder="Search Google or type a URL" aria-expanded="false" aria-live="polite">

相对 Xpath:

/html/body/ntp-app/div/div[2]/ntp-realbox/div/input

绝对 Xpath:

//*[@id="input"]

快照 A:enter image description here

快照 B:

enter image description here

selenium-webdriver xpath webdriver webdriverwait google-chrome

评论

0赞 Black cat 7/29/2023
请在调用 XPath 查找器的地方附加代码。

答:

0赞 MD Kawsar 7/29/2023 #1

我相信您可以简单地寻找 textarea 标签,如下面的 Xpath 所示。

//textarea[@type="search"]

google search page screenshot

评论

0赞 Black cat 7/29/2023
看看附图,有OP要找的东西
0赞 MD Kawsar 7/29/2023
你能给我那个页面的直接网址吗?
0赞 undetected Selenium 7/30/2023 #2

给定快照:

snapshot

您正在尝试找到手动打开的默认 chrome 浏览器页面的搜索框,该页面在 Selenium 驱动的 ChromeDriver 启动的 浏览上下文中不可用


理想情况下,您应该访问一个 URL。如果访问 url https://www.google.com/,则可以使用以下定位器策略找到搜索框:

  • 代码块:

    driver.get("https://www.google.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("BRAJESH KUMAR")
    
  • 浏览器快照:

BRAJESH KUMAR

评论

1赞 BRAJESH KUMAR 8/10/2023
是的,我一直在努力找到默认 chrome 浏览器页面的 xpath,该页面在 Selenium 驱动的 ChromeDriver 启动的 google-chrome 浏览上下文中不可用。非常感谢您的善意支持