如何点击父类如果子类的href与要求匹配

huangapple go评论130阅读模式
英文:

How click the parent class if the child href matches the requirement

问题

I've tired to find the right selenium code to get click the main parent class if the following requirements exist in the class :

  1. Parent Class
  2. <div class ="col-xs-2-4 shopee-search-item-result__item" data-sqe="item">
  3. Child class
  4. <a data-sqe="link" href= all urls that is printed in python.>
  5. Child class contains this element
  6. <div class = "_1gkBDw _2O43P5">
  7. <div class = "_1HvBLA">
  8. <div class = "_3ao649" data-sqe="ad"> Ad</div>

Here is the code bellow

  1. from selenium import webdriver
  2. from selenium.webdriver.common.action_chains import ActionChains
  3. import time
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. import csv
  10. import time
  11. url = 'https://shopee.com.my/search?keyword=mattress'
  12. driver = webdriver.Chrome(executable_path=r'E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe')
  13. driver.get(url)
  14. time.sleep(0.8)
  15. # select language
  16. driver.find_element_by_xpath('//div[@class="language-selection__list"]/button').click()
  17. time.sleep(3)
  18. # scroll few times to load all items
  19. for x in range(10):
  20. driver.execute_script("window.scrollBy(0,300)")
  21. time.sleep(0.1)
  22. # get all links (without clicking)
  23. all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
  24. print('len:', len(all_items))
  25. all_urls = []
  26. j = 0
  27. k = 45
  28. for item in all_items:
  29. url = item.get_attribute('href')
  30. all_urls.append(url)
  31. print(all_urls)
  32. a= len(all_urls)
  33. # now use links
  34. i = 0
  35. while i <= 4 :
  36. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item']//a[@class='link' and @href= all_urls[i]]"))).click()
  37. i+=1

I've tried to locate:
-Div the whole class
-locate classes and the href individually
-click the first five columns
but it all always fails.

  1. Traceback (most recent call last):
  2. File "E:/Users/Asashin/Desktop/Bots/click test 7.py", line 52, in <module>
  3. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item']//a[@class='link' and @href= all_urls[i]]"))).click()
  4. File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
  5. raise TimeoutException(message, screen, stacktrace)
  6. selenium.common.exceptions.TimeoutException: Message:

Can I be solved?

英文:

Hi I've tired to find the right selenium code to get click the main parent class if the following requirements exist in the class :

  1. Parent Class
  2. &lt;div class =&quot;col-xs-2-4 shopee-search-item-result__item&quot; data-sqe=&quot;item&quot;&gt;
  3. Child class
  4. &lt;a data-sqe=&quot;link&quot; href= all urls that is printed in python.&gt;
  5. Child class contains this element
  6. &lt;div class = &quot;_1gkBDw _2O43P5&quot;&gt;
  7. &lt;div class = &quot;_1HvBLA&quot;&gt;
  8. &lt;div class = &quot;_3ao649&quot; data-sqe=&quot;ad&quot;&gt; Ad&lt;/div&gt;

如何点击父类如果子类的href与要求匹配

Here is the code bellow

  1. from selenium import webdriver
  2. from selenium.webdriver.common.action_chains import ActionChains
  3. import time
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. import csv
  10. import time
  11. url = &#39;https://shopee.com.my/search?keyword=mattress&#39;
  12. driver = webdriver.Chrome(executable_path=r&#39;E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe&#39;)
  13. driver.get(url)
  14. time.sleep(0.8)
  15. # select language
  16. driver.find_element_by_xpath(&#39;//div[@class=&quot;language-selection__list&quot;]/button&#39;).click()
  17. time.sleep(3)
  18. # scroll few times to load all items
  19. for x in range(10):
  20. driver.execute_script(&quot;window.scrollBy(0,300)&quot;)
  21. time.sleep(0.1)
  22. # get all links (without clicking)
  23. all_items = driver.find_elements_by_xpath(&#39;//a[@data-sqe=&quot;link&quot;]&#39;)
  24. print(&#39;len:&#39;, len(all_items))
  25. all_urls = []
  26. j = 0
  27. k = 45
  28. for item in all_items:
  29. url = item.get_attribute(&#39;href&#39;)
  30. all_urls.append(url)
  31. print(all_urls)
  32. a= len(all_urls)
  33. # now use links
  34. i = 0
  35. while i &lt;= 4 :
  36. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//div[@class=&#39;col-xs-2-4 shopee-search-item-result__item&#39; and @data-sqe=&#39;item&#39;]//a[@class=&#39;link&#39; and @href= all_urls[i]]&quot;))).click()
  37. i+=1

I've tried to locate:
-Div the whole class
-locate classes and the href individualy
-click the first five columns
but it all always fails.

  1. Traceback (most recent call last):
  2. File &quot;E:/Users/Asashin/Desktop/Bots/click test 7.py&quot;, line 52, in &lt;module&gt;
  3. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//div[@class=&#39;col-xs-2-4 shopee-search-item-result__item&#39; and @data-sqe=&#39;item&#39;]//a[@class=&#39;link&#39; and @href= all_urls[i]]&quot;))).click()
  4. File &quot;C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py&quot;, line 80, in until
  5. raise TimeoutException(message, screen, stacktrace)
  6. selenium.common.exceptions.TimeoutException: Message:

Can I be solved?

答案1

得分: 1

我已经做了一些更改。

  1. 当您获取href值时,您获取的是完整的URL,而不是在DOM中看到的URL,所以您需要删除前面的部分以便以后验证。
  2. 在最后的while循环中,all_urls[i]是一个变量,您需要将它传递为变量而不是字符串。
  3. 一旦您点击每个链接,您需要使用driver.back()返回到父页面。
  1. from selenium import webdriver
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. import time
  6. url = 'https://shopee.com.my/search?keyword=mattress'
  7. driver = webdriver.Chrome(executable_path=r'E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe')
  8. driver.get(url)
  9. # 选择语言
  10. WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="language-selection__list"]/button'))).click()
  11. time.sleep(3)
  12. # 滚动几次以加载所有项目
  13. for x in range(10):
  14. driver.execute_script("window.scrollBy(0,300)")
  15. time.sleep(0.1)
  16. # 获取所有链接(不点击)
  17. all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
  18. print('len:', len(all_items))
  19. all_urls = []
  20. j = 0
  21. k = 45
  22. for item in all_items:
  23. # 这将给您锚标签的完整URL
  24. url = item.get_attribute('href')
  25. # 为了以后点击验证href,您需要删除前面的部分
  26. urlfinal = url.split('https://shopee.com.my')[1]
  27. all_urls.append(urlfinal)
  28. print(all_urls)
  29. a = len(all_urls)
  30. # 现在使用链接
  31. i = 0
  32. while i <= 4:
  33. # 通过子标签识别父标签,使用以下XPath。
  34. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item'][.//a[@data-sqe='link' and @href='" + all_urls[i] + "']]"))).click()
  35. driver.back()
  36. i += 1
英文:

I have made couple of changes.

>1. When you are fetching href values you are getting complete url and not the url you are seeing in DOM so you need to remove the preceding values in order to verify later.
>
>2. In the last while loop all_urls[i] is variable you need passed it as variable not string.
>
>3. Once you click each link you need to come back to the parent page again by using driver.back()

  1. from selenium import webdriver
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. import time
  6. url = &#39;https://shopee.com.my/search?keyword=mattress&#39;
  7. driver = webdriver.Chrome(executable_path=r&#39;E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe&#39;)
  8. driver.get(url)
  9. # select language
  10. WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,&#39;//div[@class=&quot;language-selection__list&quot;]/button&#39;))).click()
  11. time.sleep(3)
  12. # scroll few times to load all items
  13. for x in range(10):
  14. driver.execute_script(&quot;window.scrollBy(0,300)&quot;)
  15. time.sleep(0.1)
  16. # get all links (without clicking)
  17. all_items = driver.find_elements_by_xpath(&#39;//a[@data-sqe=&quot;link&quot;]&#39;)
  18. print(&#39;len:&#39;, len(all_items))
  19. all_urls = []
  20. j = 0
  21. k = 45
  22. for item in all_items:
  23. # This give you whole url of the anchor tag
  24. url = item.get_attribute(&#39;href&#39;)
  25. # You need to remove the preceding values in order to verify href later for clicking
  26. urlfinal=url.split(&#39;https://shopee.com.my&#39;)[1]
  27. all_urls.append(urlfinal)
  28. print(all_urls)
  29. a= len(all_urls)
  30. # now use links
  31. i = 0
  32. while i &lt;= 4 :
  33. #Identify the parent tag by child tag use following Xpath.
  34. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//div[@class=&#39;col-xs-2-4 shopee-search-item-result__item&#39; and @data-sqe=&#39;item&#39;][.//a[@data-sqe=&#39;link&#39; and @href=&#39;&quot; + all_urls[i] +&quot;&#39;]]&quot;))).click()
  35. driver.back()
  36. i+=1

答案2

得分: 0

如何获取父元素:

child_element = driver.find_element_by_xpath('//a[@data-sqe="link"]')
parent_element = child_element.find_element_by_xpath('./ancestor::divcontains(@class, "shopee-search-item-result__item")')

如何获取具有特定子元素的元素:

element = driver.find_element_by_xpath('div[contains(@class, "shopee-search-item-result__item") and .//a[@data-sqe="link"]]')

英文:

How to get parent element:

  1. child_element = driver.find_element_by_xpath(&#39;//a[@data-sqe=&quot;link&quot;]&#39;)
  2. parent_element = child_element.find_element_by_xpath(&#39;./ancestor::div[contains(@class, &quot;shopee-search-item-result__item&quot;)][1]&#39;)

How to get element with specific child:

  1. element = driver.find_element_by_xpath(&#39;div[contains(@class, &quot;shopee-search-item-result__item&quot;) and .//a[@data-sqe=&quot;link&quot;]]&#39;)

huangapple
  • 本文由 发表于 2020年1月3日 14:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定