Although I am newbie on Python, I have searched for several hours before addressing this question.
Would you let me know how to properly find the XPATH from website www.marinetraffic.com search?
I think the search box might be in a iframe, but I was unable to make it work (even after deeply checking/learning with the "SelectorsHub" extension.
My latest code
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
Path = "C:\Selenium\chromedriver.exe"
S = Service(Path)
driver = webdriver.Chrome(service = S)
#Abrindo Marine Traffic
driver.get("http://www.marinetraffic.com")
time.sleep(5)
driver.maximize_window()
driver.find_element(By.XPATH,"(//input[@id='searchMT'])[1]").send_keys("vessel", Keys.ENTER)#
error message
Traceback (most recent call last):
File "...\MarineTraffic.py", line 33, in <module>
driver.find_element(By.XPATH,"(//input[@id='searchMT'])").send_keys("vessel", Keys.ENTER)
File "...\webdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "...\webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "...\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to
locate element: {"method":"xpath","selector":"(//input[@id='searchMT'])"}
I know this code works because other sites are responding fine (such as google, etc), but Marinetraffic.
I have tried
(By.ID, '//*[@id="searchMT"]')
(By.CSS_SELECTOR,"#searchMT")
and many others.
CodePudding user response:
I used execute script to type vessel in the search input box.
Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.marinetraffic.com/")
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='AGREE']"))).click()
except:
print("AGree button did not show up")
search = wait.until(EC.visibility_of_element_located((By.ID, "searchMarineTraffic")))
driver.execute_script("arguments[0].setAttribute('value', 'Vessel')", search)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC