在转会市场网站上用 selenium python 查找元素时出错?(尝试使用 xpath、class 名称等)但没有任何效果
Error finding element with selenium python of transfer market website?(tried with xpath,class name etc) but nothing works
嗨,亲爱的程序员,webscrapers.I 已经尝试了很多次来定位元素然后按下它,但它总是给我错误 selenium.I have treid using XPATH,class name并且元素的 id 不存在,所以我尝试了多种方法但没有任何效果并且 returns 我出错了。
这是我要废弃的网站 link。
Website to scrap
这是我想废弃的元素。
这个元素有 class selector-title 所以我尝试使用 class 名称以及这个元素的 xpath 和它的子元素但不幸的是无法找到它。
这是我 used.This 代码正在为其他功能工作的代码,比如我找到了搜索框并搜索了按下的按钮 works.Right.Where 它起作用的是我一直在尝试的元素 work.Please 让我知道我这边的错误是什么,这样这就可行了,或者我可以找到它然后单击它。
实际上,我必须这样做才能通过网络抓取俱乐部或联赛中所有球员的数据,因此我必须先 select 国家联赛,然后我才能抓取数据,所以它非常对我来说非常重要的是能够真正找到并单击标记的 element.Please 如果你们中的任何一个 can.Thanks.
帮助我
标记元素的 XPATH
//*[@id="main"]/header/div[3]/tm-quick-select-bar//div/tm-quick-select[1]/div/div
这是我的代码
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
os.environ['PATH'] += r"C:/ChromeDriver"
browser = webdriver.Chrome()
browser.get("https://www.transfermarkt.us/")
element = browser.find_element(By.XPATH, '//*[@id="schnellsuche"]/input[1]')
element.send_keys("J1 League")
time.sleep(5)
# element2 = browser.find_element(By.XPATH, '//*[@id="main"]/header/div[3]/tm-quick-select-bar//div/tm-quick-select[1]/div/div[1]/strong/text()')
# element2.click()
button = wait(browser, 10).until(EC.presence_of_element_located(
(By.CLASS_NAME, 'selector-title')))
button.click()
time.sleep(10)
我得到的错误是浏览器在到达要单击的最后一个元素目标元素后立即关闭
DevTools listening on ws://127.0.0.1:61330/devtools/browser/d6f23f8f-6bea-41dd-8500-08f85c9cb502
[18424:9224:0523/200949.930:ERROR:device_event_log_impl.cc(214)] [20:09:49.929] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[18424:9224:0523/200949.936:ERROR:device_event_log_impl.cc(214)] [20:09:49.935] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[18424:9224:0523/200949.951:ERROR:device_event_log_impl.cc(214)] [20:09:49.951] Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed.
Traceback (most recent call last):
File "d:\WeBScraping\FreecodecampWebScrapingTutorial\selenium_tutorial_freecodecamp\scrapingtarget.py", line 18, in <module>
button = wait(browser, 10).until(EC.presence_of_element_located(
File "C:\Users\bilal\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
Ordinal0 [0x00E1B8F3+2406643]
Ordinal0 [0x00DAAF31+1945393]
Ordinal0 [0x00C9C748+837448]
Ordinal0 [0x00CC92E0+1020640]
Ordinal0 [0x00CC957B+1021307]
Ordinal0 [0x00CF6372+1205106]
Ordinal0 [0x00CE42C4+1131204]
Ordinal0 [0x00CF4682+1197698]
Ordinal0 [0x00CE4096+1130646]
Ordinal0 [0x00CBE636+976438]
Ordinal0 [0x00CBF546+980294]
GetHandleVerifier [0x01089612+2498066]
GetHandleVerifier [0x0107C920+2445600]
GetHandleVerifier [0x00EB4F2A+579370]
GetHandleVerifier [0x00EB3D36+574774]
Ordinal0 [0x00DB1C0B+1973259]
Ordinal0 [0x00DB6688+1992328]
Ordinal0 [0x00DB6775+1992565]
Ordinal0 [0x00DBF8D1+2029777]
BaseThreadInitThunk [0x76CDFA29+25]
RtlGetAppContainerNamedObjectPath [0x76F57A7E+286]
RtlGetAppContainerNamedObjectPath [0x76F57A4E+238]
不确定我是否理解正确,但是下面的代码 return
以下 text
“美国”。
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
driver = webdriver.Chrome(options=options, desired_capabilities=capabilities)
# open url:
driver.get('https://www.transfermarkt.us/')
# switch to cookies frame
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "sp_message_iframe_575847")))
time.sleep(3)
# click "Accept" cookies button
driver.find_element(By.XPATH, '//button[@title="ACCEPT ALL"]').click()
time.sleep(3)
# go back to main frame
driver.switch_to.default_content()
# here's the trick, what you are looking for is inside a "shadow-root" DOM so to access it you need to execute the script and then use CSS selector, I don't think XPATH works here:
country = driver.execute_script("return document.querySelector('tm-quick-select-bar').shadowRoot.querySelector('div > tm-quick-select:nth-child(2) > div > div > strong')")
# print
print(country.text)
# close the driver
driver.close()
嗨,亲爱的程序员,webscrapers.I 已经尝试了很多次来定位元素然后按下它,但它总是给我错误 selenium.I have treid using XPATH,class name并且元素的 id 不存在,所以我尝试了多种方法但没有任何效果并且 returns 我出错了。
这是我要废弃的网站 link。
Website to scrap
这是我想废弃的元素。
这个元素有 class selector-title 所以我尝试使用 class 名称以及这个元素的 xpath 和它的子元素但不幸的是无法找到它。 这是我 used.This 代码正在为其他功能工作的代码,比如我找到了搜索框并搜索了按下的按钮 works.Right.Where 它起作用的是我一直在尝试的元素 work.Please 让我知道我这边的错误是什么,这样这就可行了,或者我可以找到它然后单击它。 实际上,我必须这样做才能通过网络抓取俱乐部或联赛中所有球员的数据,因此我必须先 select 国家联赛,然后我才能抓取数据,所以它非常对我来说非常重要的是能够真正找到并单击标记的 element.Please 如果你们中的任何一个 can.Thanks.
帮助我标记元素的 XPATH
//*[@id="main"]/header/div[3]/tm-quick-select-bar//div/tm-quick-select[1]/div/div
这是我的代码
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
os.environ['PATH'] += r"C:/ChromeDriver"
browser = webdriver.Chrome()
browser.get("https://www.transfermarkt.us/")
element = browser.find_element(By.XPATH, '//*[@id="schnellsuche"]/input[1]')
element.send_keys("J1 League")
time.sleep(5)
# element2 = browser.find_element(By.XPATH, '//*[@id="main"]/header/div[3]/tm-quick-select-bar//div/tm-quick-select[1]/div/div[1]/strong/text()')
# element2.click()
button = wait(browser, 10).until(EC.presence_of_element_located(
(By.CLASS_NAME, 'selector-title')))
button.click()
time.sleep(10)
我得到的错误是浏览器在到达要单击的最后一个元素目标元素后立即关闭
DevTools listening on ws://127.0.0.1:61330/devtools/browser/d6f23f8f-6bea-41dd-8500-08f85c9cb502
[18424:9224:0523/200949.930:ERROR:device_event_log_impl.cc(214)] [20:09:49.929] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[18424:9224:0523/200949.936:ERROR:device_event_log_impl.cc(214)] [20:09:49.935] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[18424:9224:0523/200949.951:ERROR:device_event_log_impl.cc(214)] [20:09:49.951] Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed.
Traceback (most recent call last):
File "d:\WeBScraping\FreecodecampWebScrapingTutorial\selenium_tutorial_freecodecamp\scrapingtarget.py", line 18, in <module>
button = wait(browser, 10).until(EC.presence_of_element_located(
File "C:\Users\bilal\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
Ordinal0 [0x00E1B8F3+2406643]
Ordinal0 [0x00DAAF31+1945393]
Ordinal0 [0x00C9C748+837448]
Ordinal0 [0x00CC92E0+1020640]
Ordinal0 [0x00CC957B+1021307]
Ordinal0 [0x00CF6372+1205106]
Ordinal0 [0x00CE42C4+1131204]
Ordinal0 [0x00CF4682+1197698]
Ordinal0 [0x00CE4096+1130646]
Ordinal0 [0x00CBE636+976438]
Ordinal0 [0x00CBF546+980294]
GetHandleVerifier [0x01089612+2498066]
GetHandleVerifier [0x0107C920+2445600]
GetHandleVerifier [0x00EB4F2A+579370]
GetHandleVerifier [0x00EB3D36+574774]
Ordinal0 [0x00DB1C0B+1973259]
Ordinal0 [0x00DB6688+1992328]
Ordinal0 [0x00DB6775+1992565]
Ordinal0 [0x00DBF8D1+2029777]
BaseThreadInitThunk [0x76CDFA29+25]
RtlGetAppContainerNamedObjectPath [0x76F57A7E+286]
RtlGetAppContainerNamedObjectPath [0x76F57A4E+238]
不确定我是否理解正确,但是下面的代码 return
以下 text
“美国”。
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
driver = webdriver.Chrome(options=options, desired_capabilities=capabilities)
# open url:
driver.get('https://www.transfermarkt.us/')
# switch to cookies frame
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "sp_message_iframe_575847")))
time.sleep(3)
# click "Accept" cookies button
driver.find_element(By.XPATH, '//button[@title="ACCEPT ALL"]').click()
time.sleep(3)
# go back to main frame
driver.switch_to.default_content()
# here's the trick, what you are looking for is inside a "shadow-root" DOM so to access it you need to execute the script and then use CSS selector, I don't think XPATH works here:
country = driver.execute_script("return document.querySelector('tm-quick-select-bar').shadowRoot.querySelector('div > tm-quick-select:nth-child(2) > div > div > strong')")
# print
print(country.text)
# close the driver
driver.close()