Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在selenium-python中选择texbox并搜索内容并选择第一个选项?_Python_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

如何在selenium-python中选择texbox并搜索内容并选择第一个选项?

如何在selenium-python中选择texbox并搜索内容并选择第一个选项?,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,嘿,我有这个网站: 我想搜索我的查询并通过单击它获得第一个结果url。我该怎么做?我试图单击元素并发送查询,但它给了我一些错误 代码如下: from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions from selenium.common.exceptions import TimeoutException from sele

嘿,我有这个网站: 我想搜索我的查询并通过单击它获得第一个结果url。我该怎么做?我试图单击元素并发送查询,但它给了我一些错误

代码如下:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import asyncio
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains


GOOGLE_CHROME_BIN = 'path'
CHROME_DRIVER = 'path'

async def realme_rom_search(query):
    url = "https://realmeupdater.com/"
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.binary_location = GOOGLE_CHROME_BIN
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-gpu")
    driver = webdriver.Chrome(executable_path=CHROME_DRIVER, options=chrome_options)
    driver.get(url)
    await asyncio.sleep(5)
    wait = WebDriverWait(driver, 20)
    select_ccs = "select2-device-container"
    wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, select_ccs))).click()
    elem = wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "body > span > span > span.select2-search.select2-search--dropdown > input")))
    elem.send_keys(query)
    elem.send_keys(Keys.DOWN)
    elem.send_keys(Keys.ENTER)
但我得到了这个错误:

File "<string>", line 30, in realme_rom_search
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span class="select2-selection__rendered" id="select2-device-container" role="textbox" aria-readonly="true">...</span> is not clickable at point (457, 586). Other element would receive the click: <div role="dialog" aria-live="polite" aria-label="cookieconsent" aria-describedby="cookieconsent:desc" class="cc-window cc-banner cc-type-opt-out cc-theme-classic cc-bottom cc-color-override--1142219405 " style="">...</div>
  (Session info: headless chrome=90.0.4430.212)
文件“”,第30行,在realme\u rom\u搜索中
文件“/usr/local/lib/python3.9/site packages/selenium/webdriver/remote/webelement.py”,第80行,单击
self.\u执行(命令。单击\u元素)
文件“/usr/local/lib/python3.9/site packages/selenium/webdriver/remote/webelement.py”,第633行,在
返回self.\u parent.execute(命令,参数)
文件“/usr/local/lib/python3.9/site packages/selenium/webdriver/remote/webdriver.py”,第321行,在execute中
self.error\u handler.check\u响应(响应)
文件“/usr/local/lib/python3.9/site packages/selenium/webdriver/remote/errorhandler.py”,第242行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.element ClickInterceptedException:消息:元素ClickIntercepted:元素。。。在点(457586)处不可单击。其他元素将收到单击:。。。
(会话信息:无头镀铬=90.0.4430.212)
所有我想做的是搜索和去的网址,并获得rom信息和网址,有人能帮我吗


提前感谢。

首先,您需要滚动到网页末尾,让selenium知道下拉列表在哪里

使用以下代码滚动至结束:

driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
其次,这个drop是使用UI和LI元素构建的,所以Selenium中的Select类将不起作用。您所需要做的就是将所有元素存储在一个列表中,然后您可以查找所需的元素,在字符串比较之后,您可以单击它。下面是示例代码:

代码:

driver = webdriver.Chrome("C:\\Users:\\Desktop\\Selenium+Python\\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://realmeupdater.com/")
sleep(2)
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
wait.until(EC.visibility_of_element_located((By.ID, "select2-device-container"))).click()
all_options = driver.find_elements(By.CSS_SELECTOR, "#select2-device-results li")
for option in all_options:
    if option.text =='realme 2 Pro (RMX1801EX)':
        option.click()
print("Operation done successfully")

这里有几个问题:

  • 您使用的定位器不是唯一的。在这种特定情况下,这不会导致问题,但通常最好使用唯一定位器
  • 您试图单击的元素最初不在可见屏幕上,因此首先需要将其滚动到视图中
  • 底部有一个元素块要求接受cookies。该元素当前实际上正在接受您的单击。不过,我想这可以通过将元素滚动到视图中来解决。
    因此,请尝试以下方法:
  • 这应该会有帮助。
    如果仍然不够,请尝试在此之前关闭cookies块,以便:

    from selenium.webdriver.common.action_chains import ActionChains
    
    wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, 'a[aria-label="deny cookies"]'))).click()
    
    wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, 'a[aria-label="dismiss cookie message"]'))).click()
    
    element = driver.find_element_by_css_selectorid(select_ccs)
    actions = ActionChains(driver)
    actions.move_to_element(element).perform()
    

    @杰南:很高兴它对你有用。请不要投票并接受答案。
    from selenium.webdriver.common.action_chains import ActionChains
    
    wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, 'a[aria-label="deny cookies"]'))).click()
    
    wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, 'a[aria-label="dismiss cookie message"]'))).click()
    
    element = driver.find_element_by_css_selectorid(select_ccs)
    actions = ActionChains(driver)
    actions.move_to_element(element).perform()