fix: Google Flights Nach-Feld via combobox-Selector + JS-Fallback (zweites sichtbares Input)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
aca9c7f431
commit
f8797f3e08
1 changed files with 53 additions and 17 deletions
|
|
@ -247,34 +247,70 @@ def scrape_google_flights(von, nach, tage=30, aufenthalt_tage=60,
|
||||||
'input[placeholder*="Von"]',
|
'input[placeholder*="Von"]',
|
||||||
'input[aria-label*="Where from"]',
|
'input[aria-label*="Where from"]',
|
||||||
], von_name, "Von")
|
], von_name, "Von")
|
||||||
sb.sleep(1.5) # Nach Von-Auswahl fokussiert Google das Nach-Feld automatisch
|
sb.sleep(1.5) # Warten bis Von-Auswahl abgeschlossen
|
||||||
|
|
||||||
# ── 3. Nach-Feld befüllen ───────────────────────────────────────────
|
# ── 3. Nach-Feld befüllen ───────────────────────────────────────────
|
||||||
# Erst expliziten Selektor versuchen, dann aktivem Element direkt tippen
|
|
||||||
from selenium.webdriver.common.keys import Keys as _Keys
|
from selenium.webdriver.common.keys import Keys as _Keys
|
||||||
from selenium.webdriver.common.action_chains import ActionChains as _AC
|
nach_gesetzt = False
|
||||||
nach_gesetzt = _gf_fill_field(sb, [
|
|
||||||
|
# Versuch 1: Explizite aria-label / role Selektoren
|
||||||
|
for nach_sel in [
|
||||||
|
'input[role="combobox"]', # Google nutzt combobox für Autocomplete
|
||||||
'input[aria-label*="Wohin"]',
|
'input[aria-label*="Wohin"]',
|
||||||
'input[aria-label*="Zielort"]',
|
'input[aria-label*="Zielort"]',
|
||||||
'input[aria-label*="Ziel"]',
|
'input[aria-label*="Ziel"]',
|
||||||
'input[placeholder*="Wohin"]',
|
'input[placeholder*="Wohin"]',
|
||||||
'input[aria-label*="Where to"]',
|
'input[aria-label*="Where to"]',
|
||||||
'input[aria-label*="Destination"]',
|
'input[aria-label*="Destination"]',
|
||||||
], nach_name, "Nach")
|
]:
|
||||||
|
|
||||||
if not nach_gesetzt:
|
|
||||||
# Fallback: aktives Element (Nach-Feld sollte jetzt fokussiert sein)
|
|
||||||
try:
|
try:
|
||||||
active = sb.driver.switch_to.active_element
|
# Wenn mehrere combobox-Inputs: zweiten nehmen (1. = Von, 2. = Nach)
|
||||||
active.send_keys(nach_name)
|
elems = sb.find_elements(nach_sel)
|
||||||
sb.sleep(2)
|
field = elems[1] if len(elems) >= 2 else (elems[0] if elems else None)
|
||||||
active.send_keys(_Keys.ARROW_DOWN)
|
if field and field != sb.driver.switch_to.active_element:
|
||||||
sb.sleep(0.5)
|
sb.execute_script("arguments[0].value = '';", field)
|
||||||
active.send_keys(_Keys.RETURN)
|
field.click()
|
||||||
sb.sleep(1)
|
sb.sleep(0.3)
|
||||||
print(f"[GF] Nach via active_element: {nach_name}")
|
field.send_keys(nach_name)
|
||||||
|
sb.sleep(2)
|
||||||
|
field.send_keys(_Keys.ARROW_DOWN)
|
||||||
|
sb.sleep(0.5)
|
||||||
|
field.send_keys(_Keys.RETURN)
|
||||||
|
sb.sleep(1)
|
||||||
|
print(f"[GF] Nach via {nach_sel}: {nach_name}")
|
||||||
|
nach_gesetzt = True
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Versuch 2: JS — zweites Input-Element finden und befüllen
|
||||||
|
if not nach_gesetzt:
|
||||||
|
try:
|
||||||
|
nach_field = sb.execute_script("""
|
||||||
|
var inputs = document.querySelectorAll('input[role="combobox"], input[aria-label]');
|
||||||
|
for (var i = 0; i < inputs.length; i++) {
|
||||||
|
var lbl = inputs[i].getAttribute('aria-label') || '';
|
||||||
|
if (lbl.match(/Wohin|Ziel|Destination|Where to/i)) return inputs[i];
|
||||||
|
}
|
||||||
|
// Fallback: zweites sichtbares Input
|
||||||
|
var all = Array.from(document.querySelectorAll('input')).filter(
|
||||||
|
e => e.offsetWidth > 0 && e.offsetHeight > 0);
|
||||||
|
return all[1] || null;
|
||||||
|
""")
|
||||||
|
if nach_field:
|
||||||
|
sb.execute_script("arguments[0].value = '';", nach_field)
|
||||||
|
nach_field.click()
|
||||||
|
sb.sleep(0.3)
|
||||||
|
nach_field.send_keys(nach_name)
|
||||||
|
sb.sleep(2)
|
||||||
|
nach_field.send_keys(_Keys.ARROW_DOWN)
|
||||||
|
sb.sleep(0.5)
|
||||||
|
nach_field.send_keys(_Keys.RETURN)
|
||||||
|
sb.sleep(1)
|
||||||
|
print(f"[GF] Nach via JS-Input: {nach_name}")
|
||||||
|
nach_gesetzt = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[GF] Nach active_element Fehler: {e}")
|
print(f"[GF] Nach JS-Fehler: {e}")
|
||||||
|
|
||||||
# ── 4. Suchen-Button klicken ────────────────────────────────────────
|
# ── 4. Suchen-Button klicken ────────────────────────────────────────
|
||||||
from selenium.webdriver.common.keys import Keys
|
from selenium.webdriver.common.keys import Keys
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue