我在send_keys上遇到麻烦
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path= '/home/ec2-user/chromedriver', chrome_options=options)
base_url = "https://www.xxxxxxxx.com/"
driver.get(base_url)
driver.find_elements_by_xpath("//input[@id = 'homepage_search_box']")[0].send_keys("Pink's Hot Dogs")
上面的代码在Windows中可以正常工作,但是当涉及到ubuntu时会出现类似
的错误selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: headless chrome=67.0.3396.99)
(Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.14.47-56.37.amzn1.x86_64 x86_64)
HTML:
<form class="form-inline" onsubmit="simple_search(jQuery('#homepage_search_box').val());return false;">
<div class="form-group col-md-8">
<label class="sr-only" for="homepage_search_box">Company Name or Report #</label>
<input name="homepage_search_box" id="homepage_search_box" class="form-control" placeholder="Company Name or Report #" type="text">
</div>
<div class="form-group col-md-4">
<button type="submit" class="btn btn-default">Search</button>
</div>
</form>
任何人都可以帮助
driver.find_elements_by_xpath("//input[@id = 'homepage_search_box']")[0]
它完美地赋予了硒对象 我在发送密钥时出错
答案 0 :(得分:1)
要将字符序列发送到搜索框,您需要诱使 WebDriverwait 使所需的元素可点击,并且您可以使用以下任一方法以下解决方案:
代码块:
# -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
options.add_argument('--headless')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.ripoffreport.com/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.header-field#header-search-text"))).send_keys("Pink's Hot Dogs")
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='header-field'][@id='header-search-text']"))).send_keys("Pink's Hot Dogs")
print("Search Text Sent")
控制台输出:
Search Text Sent
答案 1 :(得分:0)
无法复制实际的URL,但您可以尝试以下操作:
希望这会有所帮助!
答案 2 :(得分:0)
您可以尝试使用javaScript
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement);
executor.executeScript("arguments[0].setAttribute('value', '"+text which you want to enter+"');",webElement);