Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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
Python 通过不在headless模式下工作的xpath查找元素_Python_Html_Selenium_Google Chrome - Fatal编程技术网

Python 通过不在headless模式下工作的xpath查找元素

Python 通过不在headless模式下工作的xpath查找元素,python,html,selenium,google-chrome,Python,Html,Selenium,Google Chrome,我在python中使用selenium,当我在非headless模式下运行它时,下面的代码工作正常,但当我切换到headless模式时,它会给我以下错误: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method&quo

我在python中使用selenium,当我在非headless模式下运行它时,下面的代码工作正常,但当我切换到headless模式时,它会给我以下错误:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"xpath","selector":"//div[.='Renee']"}
(Session info: headless chrome=89.0.4389.82)
代码:

from seleniumwire import webdriver 
import sys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time
import pyaudio
import wave
import pyperclip
from selenium.webdriver.chrome.options import Options
import warnings


warnings.simplefilter(action='ignore', category=FutureWarning)    


browser_locale = 'fr'
chrome_options = Options()
#chrome_options.headless = True
chrome_options.add_argument("--headless")
chrome_options.add_argument("--lang={}".format(browser_locale))
# for linux/Ubuntu only
chrome_options.add_argument("--no-sandbox") 


browser = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
browser.get('https://www.ibm.com/demos/live/tts-demo/self-service/home')
voice = browser.find_element_by_xpath('//*[@id="downshift-2-toggle-button"]/span')
voice.click()
browser.find_element_by_xpath("//div[.='Renee']").click()
search = browser.find_element_by_xpath('//*[@id="text-area"]')
search.clear()
search.send_keys("text here")
任何帮助都将不胜感激

browser_locale = 'fr'

options = webdriver.ChromeOptions()
options.add_argument("--lang={}".format(browser_locale))
#options.headless = True

options.add_argument("--headless")

browser = webdriver.Chrome(options=options)

browser.get('https://www.ibm.com/demos/live/tts-demo/self-service/home')
browser.find_element_by_xpath('//*[@aria-labelledby="downshift-0-label downshift-0-toggle-button"]').click()
browser.find_element_by_xpath(
    '//*[@class="bx--list-box__menu-item__option" and contains(text(),"German")]').click()
browser.find_element_by_xpath(
    '//*[@aria-labelledby="downshift-0-label downshift-0-toggle-button"]').click()
browser.find_element_by_xpath(
    '//*[@class="bx--list-box__menu-item__option" and contains(text(),"French")]').click()
voice = browser.find_element_by_xpath(
    '//*[@id="downshift-2-toggle-button"]/span')
voice.click()

browser.find_element_by_xpath("//div[.='Renee']").click()

search = browser.find_element_by_xpath('//*[@id="text-area"]')
search.clear()
search.send_keys("text here")

只需再次选择语言即可启用下拉列表,在无头模式下显示为禁用,截图并检查

像魅力一样工作,非常感谢您抽出时间