简体   繁体   English

如何确定使用Selenium和Python查找并打开网页上的所有配置文件的确切xpath?

[英]How do I decide what the exact xpath is to find and open all profiles on a web page using Selenium and Python?

I am new to programming, and I am trying to build a test software to automate the data retrieval from a website. 我是编程的新手,我正在尝试构建一个测试软件以自动从网站检索数据。 I am, however, finding it difficult to define the xpath, so that all of the individual profiles can be found and clicked on. 但是,我发现很难定义xpath,因此可以找到并单击所有单独的配置文件。

'/html/body/div[1]/div[2]/div/div[3]/div/main/table[2]/tbody/tr/td/div/table/tbody/**tr[2]/td[1]**/div/table/tbody/tr/td[1]/a'
'/html/body/div[1]/div[2]/div/div[3]/div/main/table[2]/tbody/tr/td/div/table/tbody/**tr[2]/td[2]**/div/table/tbody/tr/td[1]/a'
'/html/body/div[1]/div[2]/div/div[3]/div/main/table[2]/tbody/tr/td/div/table/tbody/**tr[3]/td[1]**/div/table/tbody/tr/td[1]/a'

I am finding it difficult, because the variables are in the middle of the xpath and don't know how to write the profilesCount and the for loop. 我发现这很困难,因为变量位于xpath的中间,并且不知道如何编写profilesCount和for循环。 Sorry, if my question sounds dumb, I have read all resources I could think of, but found no solution. 抱歉,如果我的问题听起来很愚蠢,我已经阅读了所有可以想到的资源,但没有找到解决方案。 Thank you for your help in advance. 提前谢谢你的帮助。

tell me if I am wrong 告诉我我是否错

if I got it right, you want to make a loop thats going to check all profiles on website .... If its the same problem as I had in my proxy_gatherer, then this solution may help you: 如果我做对了,您想做一个循环来检查网站上的所有配置文件...。如果它与我在proxy_gatherer中遇到的问题相同,那么此解决方案可能会帮助您:

for x in range(2,34):
    try:
        u1 = browser.find_element_by_xpath('//*[@id="tbl_proxy_list"]/tbody[1]/tr[' + str(x) + ']/td[1]/abbr').text
        except:
            print('proxy out of range')

in your case ( based on all informations you provided ): 您的情况(根据您提供的所有信息):

for x in range(2,last_profile_id):
    for xx in range(1,2):
        browser.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[3]/div/main/table[2]/tbody/tr/td/div/table/tbody/**tr[{}]/td[{}]**/div/table/tbody/tr/td[1]/a'.format(x,xx))

explanation what i did: based on xpaths you provided I analysed which parts are changing ... here it was : 解释我做了什么:根据您提供的xpath,我分析了哪些部分正在发生变化……这里是:

// tr[2]/td[1] // -> its written bold here : // tr [2] / td [1] //->此处以粗体显示:

'/html/body/div[1]/div[2]/div/div[3]/div/main/table[2]/tbody/tr/td/div/table/tbody/ tr[2]/td[1] /div/table/tbody/tr/td[1]/a' '/ html / body / div [1] / div [2] / div / div [3] / div / main / table [2] / tbody / tr / td / div / table / tbody / tr [2] / td [1] / div / table / tbody / tr / td [1] / a'

I hope I helped you :) if no and I completely missunderstood what did you ask, please let me know ^^ 希望我能对您有所帮助:)如果没有,我完全会误解了您的要求,请告诉我^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM