Selenium 只能在 Python 中找到某些元素

我在 Python 中使用 Selenium 查找元素时遇到了一些问题,它适用于我测试过的所有其他网站上的每个元素,但在游戏网站上它只能找到某些元素。

这是我正在使用的代码:

from selenium import webdriver
import time

driver = webdriver.Chrome("./chromedriver")
driver.get("https://www.jklm.fun")

passSelf = input("Press enter when in game...")
time.sleep(1)
syllable = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[2]/div").text
print(syllable)

运行代码后,未找到元素 /html/body/div[2]/div[2]/div[2]/div[2]/div。在图像中,您可以看到它试图找到的元素:

Element the code is trying to find

然而,运行相同的代码,但用主游戏之外的东西(例如右上角的房间代码)替换了 XPath,它成功地找到了元素:

Output of the code being run on a different element

我已经尝试使用类名、名称、选择器和 XPath 来查找原始元素,但我认为影响它的唯一因素是:

  1. 元素会定期变化(不确定这是否会影响它)
  2. 元素位于“画布区域”中,但它以某种方式阻止了它。

我不确定这些事情是否重要,因为我是使用 selenium 的新手,任何帮助都值得赞赏。游戏所在的网站是https://www.jklm.fun/,如果您想浏览一下元素

cjq790323 回答:Selenium 只能在 Python 中找到某些元素

您尝试访问的元素位于 iframe 内。像这样先切换到框架

driver.switch_to_frame(driver.find_element_by_xpath("//div[@class='game']/iframe[contains(@src,'jklm.fun')]"))
,
import requests
import json

url = 'https://ifconfig.co/json'

#http://user:password@host/


proxyList = []
with open('proxies.txt','r') as f:
   for line in f:
       line = line.replace('\n','')
       tmp = line.split(':')
       proxies = {
           'http': 'http://'+ tmp[0] + ':' + tmp[1] + '@' + tmp[2] + ':' + tmp[3] + '/','https': 'http://'+ tmp[0] + ':' + tmp[1] + '@' + tmp[2] + ':' + tmp[3] + '/',}
       proxyList.append(proxies)


for proxy in proxyList:    
   session = requests.Session()
   r = session.get(url,proxies=proxy)
   print(r.json()['ip'])



本文链接:https://www.f2er.com/842.html

大家都在问