NoSuchElementException และ SyntaxError ระหว่างการขูดเว็บ
ฉันมีปัญหาในการใช้คำแนะนำนี้เพื่อแก้ไขข้อผิดพลาดต่อไปนี้:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
ได้เมื่อฉันใช้รหัสต่อไปนี้:
from selenium import webdriver
query = ' I want to try to translate this text'
chrome_options = webdriver.ChromeOptions('/chromedriver')
driver = webdriver.Chrome()
driver.get('https://translate.google.com/')
search = driver.find_element_by_css_selector('#source')
search.send_keys(query)
search.submit()
ตามที่อธิบายไว้ที่นี่: NoSuchElementException - ไม่พบองค์ประกอบฉันควรใช้สิ่งนี้
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("source"));
แต่ฉันได้รับ SyntaxError (เนื่องจากWebDriverWait wait =)
ฉันได้ลองทำตามคำตอบเหล่านี้แล้ว:
NoSuchElementException (SyntaxError: บล็อกที่ซ้อนกันแบบคงที่มากเกินไป)
Selenium Webdriver - NoSuchElementExceptions
แต่ฉันยังคงได้รับข้อผิดพลาด:
try:
search = driver.find_element_by_css_selector('#source')
break
except NoSuchElementException:
time.sleep(1)
ให้ฉันbreak outside the loop; ในขณะที่สิ่งนี้
try:
search = driver.find_element_by_css_selector('#source')
except NoSuchElementException:
pass
ไม่ได้เปลี่ยนแปลงอะไร (ยังคงทำให้ผมมีข้อผิดพลาด: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"})
คุณช่วยฉันหาวิธีแก้ไขข้อผิดพลาดเหล่านี้ได้ไหม
อัปเดต: ฉันพยายามใช้ด้วยdriver.implicitly_wait(60)และพบNoSuchElementExpectionข้อผิดพลาดเดียวกัน
รายละเอียดเพิ่มเติมเกี่ยวกับข้อผิดพลาด:
---> 23 search.submit()
24
25
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in submit(self)
83 """Submits a form."""
84 if self._w3c:
---> 85 form = self.find_element(By.XPATH, "./ancestor-or-self::form")
86 self._parent.execute_script(
คำตอบ
search.submit()คุณได้ทำทุกอย่างไรท์ในรหัสของคุณที่ใช้ร่วมกันที่ด้านบนยกเว้นบรรทัดนี้ ในขณะที่คุณกำลังเรียกsubmit()วิธีการขององค์ประกอบเว็บและองค์ประกอบการค้นหาที่กำหนดโดยเป็นรูปแบบที่คุณไม่ค่อนข้างมันจึงTextarea NoSuchElementExceptionเนื่องจากวิธีการส่งใช้ได้กับformองค์ประกอบประเภทเดียวเท่านั้น หากคุณลบบรรทัดนี้รหัสของคุณจะใช้งานได้ดี
จาก webdriver นำเข้าซีลีเนียม
query = ' I want to try to translate this text'
chrome_options = webdriver.ChromeOptions('/chromedriver')
driver = webdriver.Chrome()
driver.get('https://translate.google.com/')
search = driver.find_element_by_css_selector('#source')
search.send_keys(query)
เอาต์พุต
บันทึก :
หากต้องการทราบวิธีใช้กลไกการรอที่แตกต่างกันในซีลีเนียมหลามด้านล่างนี้สามารถอ่านได้ดี:
https://selenium-python.readthedocs.io/waits.html
<textarea id="source" class="orig tlid-source-text-input goog-textarea" rows="1" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" style="overflow: auto hidden; box-sizing: border-box; height: 70px; padding-bottom: 18px;"></textarea>
Xpaths สามารถเป็นได้:
//*[@id='source']
/html/body/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[1]/textarea
โดยทั่วไปรอองค์ประกอบและส่งแบบสอบถามและกดส่ง
search = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, //*[@id='source']))
search.send_keys(query)
search.submit()
เพิ่มสิ่งเหล่านี้ด้วย
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC